43

I have my application that is displaying images with different ratio, resized inside (centerInside) imageView. What I need is to create bitmap from the ImageView including the background (black in this case).

So for example I have device screen 320x480, full screen imageView with image resized to 280x480. How could I get 320x480 bitmap from it?

On top of this imageview I have some logos or buttons that I don't want to include to bitmap, they're like on top layer. All I need is bitmap with image and black border from some sides.

yosh
  • 3,245
  • 7
  • 55
  • 84

7 Answers7

98

You could just use the imageView's image cache. It will render the entire view as it is layed out (scaled,bordered with a background etc) to a new bitmap.

just make sure it built.

imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();

there's your bitmap as the screen saw it.

Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
  • 2
    Sounds good but it just pops the crit with NullPointerException. It's because the image is not built yet? – yosh Jan 18 '11 at 11:14
  • 6
    Ok, along with http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null it solved my problem :) Thanks. – yosh Jan 18 '11 at 16:06
  • 5
    The documentation states `buildDrawingCache()` should be folloed by a call to `destroyDrawingCache` (see here: http://developer.android.com/reference/android/view/View.html#buildDrawingCache%28boolean%29) – Herr von Wurst Sep 13 '13 at 12:20
  • 3
    It is deprecated. – Michalsx Aug 17 '18 at 06:18
  • Yep at this point there is no more need for the drawing cache. This answer didn't age well :) – Greg Giacovelli May 26 '22 at 19:34
79

Have you tried:

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • It gives the size of image but that's not the problem. I need some fast way to create bitmap along with black border. Here I guess I have to calculate the orientation and middle of the image and both black borders to add them manually to bitmap created from the image. – yosh Jan 17 '11 at 16:07
  • Definitely the best solution, if you want to get the actual contents of an ImageView and not just the visible parts. – Michell Bak Jul 05 '12 at 10:42
  • Note that imageView.getDrawingCache() will get the actual bitmap representation,the bitmap would be lost when the cache is cleared. – secretlm Jan 07 '13 at 10:14
  • @Cristian m using your code to convert imageview to bitmap, and on button click i want to set that bitmap on surfaceview can you guide me how to do this, when i set bitmap on surfaceview it opens up a black screen. How to handle this – Sunishtha Singh May 07 '15 at 06:48
  • `VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable` – Dr.jacky Sep 08 '22 at 21:22
4

Just thinking out loud here (with admittedly little expertise working with graphics in Java) maybe something like this would work?:

ImageView iv = (ImageView)findViewById(R.id.imageview);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
iv.draw(canvas);

Out of curiosity, what are you trying to accomplish? There may be a better way to achieve your goal than what you have in mind.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 1
    I need to slice whole screen and want to have black edges when there is for example only half image on tile + I want to have image centered on screen. Canvas brings back some bad J2ME memories :D – yosh Jan 17 '11 at 16:29
  • Maybe I'm misunderstanding...are you just wanting to tile the image? You can define a bitmap drawable in XML that will tile itself automatically. I don't exactly understand what you're asking. – Kevin Coppock Jan 17 '11 at 16:42
  • I want to slice images to squares (i.e. 50x50) including the black background. Images have different colors/ratios. I thought fast way would be create bitmap from whole ImageView, not only the image content. – yosh Jan 18 '11 at 11:12
3

This is a working code

imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
2
try {
        photo.setImageURI(Uri.parse("Location");
        BitmapDrawable drawable = (BitmapDrawable) photo.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        bitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);
        photo.setImageBitmap(bitmap);

    } catch (Exception e) {

    }
0

It works in Kotlin after buildDrawingCache() being deprecated

 // convert imageView to bitmap
val bitmap = (imageViewId.getDrawable() as BitmapDrawable).getBitmap()
0

In Kotlin;

imageView.drawable?.let {
        val mBitmap = (it as BitmapDrawable).bitmap
    }
Ahmet B.
  • 1,290
  • 10
  • 20