Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to convert whole ImageView to Bitmap?

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.

like image 744
yosh Avatar asked Jan 17 '11 15:01

yosh


People also ask

How to convert Image of imageView to Bitmap android?

just make sure it built. imageView. buildDrawingCache(); Bitmap bmap = imageView. getDrawingCache();

How do I get bitmap from imageView?

Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap(); Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)


4 Answers

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.

like image 148
Greg Giacovelli Avatar answered Sep 18 '22 06:09

Greg Giacovelli


Have you tried:

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); Bitmap bitmap = drawable.getBitmap(); 
like image 27
Cristian Avatar answered Sep 22 '22 06:09

Cristian


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.

like image 21
Kevin Coppock Avatar answered Sep 20 '22 06:09

Kevin Coppock


This is a working code

imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
like image 21
Adil Raza Bangash Avatar answered Sep 21 '22 06:09

Adil Raza Bangash