Android: how to convert whole ImageView to Bitmap?
Asked Answered
H

8

44

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.

Haygood answered 17/1, 2011 at 15:48 Comment(0)
J
79

Have you tried:

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Jermainejerman answered 17/1, 2011 at 15:54 Comment(5)
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.Haygood
Definitely the best solution, if you want to get the actual contents of an ImageView and not just the visible parts.Quoits
Note that imageView.getDrawingCache() will get the actual bitmap representation,the bitmap would be lost when the cache is cleared.Hebetic
@Jermainejerman 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 thisChitchat
VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawableFeudalize
T
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.

Tko answered 17/1, 2011 at 19:48 Comment(5)
Sounds good but it just pops the crit with NullPointerException. It's because the image is not built yet?Haygood
Ok, along with #2339929 it solved my problem :) Thanks.Haygood
The documentation states buildDrawingCache() should be folloed by a call to destroyDrawingCache (see here: developer.android.com/reference/android/view/…)Photojournalism
It is deprecated.Balboa
Yep at this point there is no more need for the drawing cache. This answer didn't age well :)Tko
J
79

Have you tried:

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Jermainejerman answered 17/1, 2011 at 15:54 Comment(5)
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.Haygood
Definitely the best solution, if you want to get the actual contents of an ImageView and not just the visible parts.Quoits
Note that imageView.getDrawingCache() will get the actual bitmap representation,the bitmap would be lost when the cache is cleared.Hebetic
@Jermainejerman 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 thisChitchat
VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawableFeudalize
M
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.

Morez answered 17/1, 2011 at 16:13 Comment(3)
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 :DHaygood
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.Morez
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.Haygood
M
3

This is a working code

imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
Monogram answered 21/2, 2017 at 6:22 Comment(1)
Its deprecated.Whitmer
P
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) {

    }
Prau answered 25/6, 2013 at 2:16 Comment(0)
A
1

As buildDrawingCache() is deprecated, From this SO answer, we need to get bitmap directly from any android View.

Example:

fun getBitmapFromView(view: View): Bitmap {
    val bitmap = Bitmap.createBitmap(
        view.width, view.height, Bitmap.Config.ARGB_8888
    )
    val canvas = Canvas(bitmap)
    view.draw(canvas)
    return bitmap
}

fun getBitmapFromView(view: View, defaultColor: Int): Bitmap {
    val bitmap = Bitmap.createBitmap(
        view.width, view.height, Bitmap.Config.ARGB_8888
    )
    val canvas = Canvas(bitmap)
    canvas.drawColor(defaultColor)
    view.draw(canvas)
    return bitmap
}

Use like this way:

//@param rootView is View object which you want to get bitmap
Bitmap bitmap = getBitmapFromView(rootView);
Bitmap bitmapColored = getBitmapFromView(rootView, Color.WHITE);
Adhesion answered 8/12, 2023 at 11:47 Comment(0)
G
0

It works in Kotlin after buildDrawingCache() being deprecated

 // convert imageView to bitmap
val bitmap = (imageViewId.getDrawable() as BitmapDrawable).getBitmap()
Greig answered 17/5, 2019 at 2:50 Comment(1)
This line is crashing my emulator. Bummed.Rembert
D
0

In Kotlin;

imageView.drawable?.let {
        val mBitmap = (it as BitmapDrawable).bitmap
    }
Drawstring answered 5/7, 2021 at 4:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.