给定
ImageView image = R.findViewById(R.id.imageView);
image.setImageBitmap(someBitmap);
是否有可能检索位图?
给定
ImageView image = R.findViewById(R.id.imageView);
image.setImageBitmap(someBitmap);
是否有可能检索位图?
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
image.getDrawable()
是否真的可以被转换为 BitmapDrawable
(以避免出现 IllegalCastExceptions
异常)。如果您的图像中使用了层,则此代码段会稍有不同:Bitmap bitmap = ((BitmapDrawable)((LayerDrawable)image.getDrawable()).getDrawable(0)).getBitmap();
。 - Alex SemeniukimageView.setImageUri()
这个方法如果是从 URI
设置 ImageView
的图片,是否有效? - Hendra Anggrian以下代码可以从 ImageView
获取一个 Bitmap
对象。然而,它不是你所设置的那个 bitmap 对象。它是一个新的对象。
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
=== 编辑 ===
imageView.setDrawingCacheEnabled(true);
imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
imageView.layout(0, 0,
imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
imageView.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false);
Bitmap bmap = Bitmap.createBitmap(mImageView.getDrawingCache());
- Azurespot对于那些正在寻找使用 Kotlin
获取 ImageView
中的 Bitmap
的解决方案的人。
var bitmap = (image.drawable as BitmapDrawable).bitmap
请写下面的代码
ImageView yourImageView = (ImageView) findViewById(R.id.yourImageView);
Bitmap bitmap = ((BitmapDrawable)yourImageView.getDrawable()).getBitmap();
这段代码更好。
public static byte[] getByteArrayFromImageView(ImageView imageView)
{
BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable());
Bitmap bitmap;
if(bitmapDrawable==null){
imageView.buildDrawingCache();
bitmap = imageView.getDrawingCache();
imageView.buildDrawingCache(false);
}else
{
bitmap = bitmapDrawable .getBitmap();
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
Bitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.jellybean_statue);
- Ahmad AghazadehBitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.jellybean_statue);
imageView.setImageBitmap(imagenAndroid);
Bitmap bitmap;
bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();