0

I referred to this SO question which suggested getting a Bitmap from an ImageView by just doing this :

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

The problem I am facing is, when I first set the background of an ImageView, followed by src and then using the suggested solution, I only get the bitmap as the src image. It didn't contained the background part of that ImageView. What am I missing here?

Community
  • 1
  • 1
Shubham A.
  • 2,446
  • 4
  • 36
  • 68
  • FYI http://stackoverflow.com/questions/8306623/get-bitmap-attached-to-imageview – Chintan Khetiya Aug 03 '16 at 06:15
  • @ChintanKhetiya Did you even read the question? The fourth word of this question provides the same question link as you provided which **isn't** working for me. – Shubham A. Aug 03 '16 at 09:18
  • Your accepted answer is same answer which is already pointed by you via link and that's why i again point to check it to be more clear. That's it. – Chintan Khetiya Aug 03 '16 at 09:30

2 Answers2

1

Try to get bitmap using DrawingCache.. try following code:

image.setDrawingCacheEnabled(true);

// this is the important code :)  
// Without it the view will have a dimension of 0,0 and the bitmap will be null          
image.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
image.layout(0, 0, image.getMeasuredWidth(), image.getMeasuredHeight()); 

image.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(image.getDrawingCache());
image.setDrawingCacheEnabled(false); // clear drawing cache
Hello World
  • 2,764
  • 1
  • 11
  • 23
  • This worked. Just that, with line `image.setDrawingCacheEnabled(false);`, it took time. When I removed this, lag was gone. However it worked in both cases. Thank you ! – Shubham A. Aug 03 '16 at 06:02
0

You can get layer of images by

Bitmap bitmap = ((BitmapDrawable)((LayerDrawable)imageViewPlayPause.getDrawable()).getDrawable(0)).getBitmap(‌​);

get images by index 0 and 1, and check which image you get.

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
Gaurav
  • 1,965
  • 2
  • 16
  • 32