How to convert a imageview to bitmap image in Android
I have an imageview.
LoaderImageView image=new LoaderIma开发者_StackOverflowgeView(context,path1);
in above statement it returns a imageview. So I want to convert it into a bitmap. How can I convert imageview into bitmap image.
ImageView i=new ImageView(context);
i.setImageBitmap(convertimagetobitmap);
In above statement I have set a bitmap image (converted image) to another imageview.
Take a look this code:
// i is an imageview which you want to convert in bitmap
Bitmap viewBitmap = Bitmap.createBitmap(i.getWidth(),i.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap);
i.draw(canvas);
That's it, your imageview is stored in bitmap viewBitMap.
Look at:
ImageView.getDrawingCache();
But you have to keep in mind that returned Bitmap
won't be the same as you get from resource or file, since Bitmap
will depend on display physical properties (resolution, scaling, color deepness and so on).
Check this BitmapFactory
This gets you access to bitmap where view is stored DrawingCache (cash has to be turned on)
Hope this helps!
This will work :
ImageView img;
img.setDrawingCacheEnabled(true);
Bitmap scaledBitmap = img.getDrawingCache();
//Store to tmp file
String extr = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extr + "/tmpfolder");
if (!mFolder.exists()) {
mFolder.mkdir();
}
String s = "tmp.png";
File f = new File(mFolder.getAbsolutePath(), s);
strMyImagePath = f.getAbsolutePath();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
scaledBitmap.compress(Bitmap.CompressFormat.PNG, 70, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
scaledBitmap.recycle();
} catch (Throwable e) {
}
精彩评论