JamesEX
JamesEX

Reputation: 125

How to take screen shot of a ImageView?

Hello i'm trying to take a screen shot of an Imageview that i have some drawing on it. I'm able to do the drawing part and saving part because I used the

 "View v = view.getRootView();"

To caputure the whole screen before and i see that it works because it can see it in gallery.

However i need to only capture the screenshot of a imageview at the moment(not whole screen) and i'm having trouble. When my user press the save button it want it to create a bmp of just the imageview. Currently it captures a blackscreen for me with my two method before. I thought it got the height and width right and it still doesn't work.

i'm getting my method 2 ideas from Taking a "screenshot" of a specific layout in Android but it is not working even though a lot of other questions are similar to it and i checked them all out.

//In constructor to let u guys know how i set up the imageView
imageView.setImageBitmap(bitmap);
...
imageView = (ImageView) this.findViewById(R.id.ImageView);
...

Method 1 to capture screen shot of the imageView

Bitmap bm = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888);

method 2 to capture screen shot of the imageView

View z = (ImageView) findViewById(R.id.ImageView);
               z.setDrawingCacheEnabled(true);   
               int totalHeight = z.getHeight();
               int totalWidth = z.getWidth();
                z.layout(0, 0, totalWidth, totalHeight);       
                z.buildDrawingCache(true);
                Bitmap bm = Bitmap.createBitmap(z.getDrawingCache());             
                z.setDrawingCacheEnabled(false);
                    Toast.makeText(Draw.this, "Taking Screenshot", Toast.LENGTH_SHORT).show();  
                    MediaStore.Images.Media.insertImage(getContentResolver(), bm, null, null); 

XML here:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/RL"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".Draw" >

    <ImageView
        android:id="@+id/ImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnLoad2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/BtnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/ImageView"
        android:layout_alignTop="@+id/btnLoad2"
        android:text="Save Picture" />

    <Button
        android:id="@+id/btnLoad2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ImageView"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        android:layout_toLeftOf="@+id/BtnSave"
        android:text="@string/Load" />

</RelativeLayout>

This is screenshot of eclipse that was displayed on the emulator then drawned with one line at the top then saved and loaded back to the imageView

Upvotes: 1

Views: 3864

Answers (1)

SpecialTrooper
SpecialTrooper

Reputation: 104

Why are you calling z.buildDrawingCache(true) ?

This should be sufficent to create a bitmap of your View

View z = (ImageView) findViewById(R.id.ImageView);
z.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());

Upvotes: 1

Related Questions