从 Android L 中的 ImageView 获取位图

我想从 ImageView 获取 Bitmap.我使用了以下代码,但 getDrawable() 返回 null.如何从 ImageView 获取整个 Bitmap.

位图位图;if (mImageViewer.getDrawable() instanceof BitmapDrawable) {位图 = ((BitmapDrawable) mImageViewer.getDrawable()).getBitmap();} 别的 {可绘制 d = mImageViewer.getDrawable();位图 = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);画布画布=新画布(位图);d.draw(画布);}storeImage(位图,"final.jpeg");

解决方案

试试这个:

<块引用>

imageView.invalidate();BitmapDrawable 可绘制 = (BitmapDrawable) imageView.getDrawable();位图 bitmap = drawable.getBitmap();

I want to get Bitmap from ImageView. I have used following code, but getDrawable() returns null. How to get whole Bitmap from ImageView.

Bitmap bitmap;
if (mImageViewer.getDrawable() instanceof BitmapDrawable) {
    bitmap = ((BitmapDrawable) mImageViewer.getDrawable()).getBitmap();
} else {
    Drawable d = mImageViewer.getDrawable();
    bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    d.draw(canvas);
}
storeImage(bitmap,"final.jpeg");

解决方案

Try this:

imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();

相关文章