1

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

Community
  • 1
  • 1
JamesEX
  • 125
  • 2
  • 7

1 Answers1

1

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());
  • This sort of worked, after i changed the code a bit. I'm able to capture the imageView with my drawing on it. However there's strangly two black bars running down the left and right side of the image. I don't have black bars on my app, so i'm not sure where they came from...i'll try it again after i get the width. – JamesEX Nov 16 '13 at 00:54
  • Could you maybe add a screenshot of your ImageView? – SpecialTrooper Nov 16 '13 at 01:04
  • I just added Screenshot. – JamesEX Nov 16 '13 at 01:21
  • Maybe the `paddingLeft` and `paddingRight` settings of your RelativeLayout could cause this problem .. remove them and try it again – SpecialTrooper Nov 16 '13 at 01:29