【发布时间】:2012-01-08 13:09:45
【问题描述】:
给定
ImageView image = R.findViewById(R.id.imageView);
image.setImageBitmap(someBitmap);
是否可以检索位图?
【问题讨论】:
-
是的,有可能当您点击图片时,如果您需要此要求,请告诉我。
标签: android imageview android-bitmap
给定
ImageView image = R.findViewById(R.id.imageView);
image.setImageBitmap(someBitmap);
是否可以检索位图?
【问题讨论】:
标签: android imageview android-bitmap
对于那些正在寻找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);
这将为您从ImageView 获取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());
试试这个代码:
Bitmap bitmap;
bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
【讨论】:
获取图像位图的其他方法是这样做:
Bitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.jellybean_statue);
imageView.setImageBitmap(imagenAndroid);
【讨论】:
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
【讨论】:
image.getDrawable() 是否真的可以转换为BitmapDrawable(以避免IllegalCastExceptions)。例如,如果您在图像中使用图层,那么这个 sn-p 将略有不同:Bitmap bitmap = ((BitmapDrawable)((LayerDrawable)image.getDrawable()).getDrawable(0)).getBitmap();
ImageView 中的图像是从URI 设置的,这是否有效? imageView.setImageUri()