获取与ImageView关联的位图

324

给定

ImageView image = R.findViewById(R.id.imageView);
image.setImageBitmap(someBitmap);

是否有可能检索位图?


1
是的,当您单击图像时,我们将得到它。如果您需要此要求,请告诉我。 - RajaReddy PolamReddy
7个回答

826
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

34
请注意检查您的 image.getDrawable() 是否真的可以被转换为 BitmapDrawable(以避免出现 IllegalCastExceptions 异常)。如果您的图像中使用了层,则此代码段会稍有不同:Bitmap bitmap = ((BitmapDrawable)((LayerDrawable)image.getDrawable()).getDrawable(0)).getBitmap(); - Alex Semeniuk
2
有时,这会返回一个带有一些或全部黑色像素的位图。 - user153275
2
如果你在drawable/imageview上应用了过滤器,那么不会返回原始位图或过滤后的位图。 - DearDhruv
4
imageView.setImageUri() 这个方法如果是从 URI 设置 ImageView 的图片,是否有效? - Hendra Anggrian
1
@praneethkumar 在我的情况下确实有效。为这个棒极了的答案点赞! - Hendra Anggrian
显示剩余2条评论

46

以下代码可以从 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);

当它“不起作用”时,会发生什么?它会返回null还是抛出异常或其他情况? - Sarwar Erfan
2
它返回null。有时我必须重新加载页面才能真正出现。 - lemon
3
代码出现了空指针异常。在这一行:Bitmap bmap = Bitmap.createBitmap(mImageView.getDrawingCache()); - Azurespot
drawingCache在Kotlin中已被弃用。 - Jeff Bootsholz

3

对于那些正在寻找使用 Kotlin 获取 ImageView 中的 Bitmap 的解决方案的人。

var bitmap = (image.drawable as BitmapDrawable).bitmap

我得到了“AppCompatImageView无法转换为android.graphics.drawable.BitmapDrawable”的错误。 - Billyjoker

3

请写下面的代码

ImageView yourImageView = (ImageView) findViewById(R.id.yourImageView);
Bitmap bitmap = ((BitmapDrawable)yourImageView.getDrawable()).getBitmap();

我得到了一个错误提示:AppCompatImageView 无法转换为 android.graphics.drawable.BitmapDrawable。 - Billyjoker

0

这段代码更好。

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();
    }

是imageView.getDrawable(); -> 意思是从drawable文件夹获取图片吗?如果我没记错的话.... @Ahmad - gumuruh
不,你可以使用这段代码。Bitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.jellybean_statue); - Ahmad Aghazadeh

-4
获取图像位图的另一种方法是这样做:
Bitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.jellybean_statue);
imageView.setImageBitmap(imagenAndroid);

-10
尝试一下这段代码:
Bitmap bitmap;
bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

6
你能否描述一下对@Arslan已被接受的答案进行的改进? - bummi
你最好解释一下你的答案是如何解决他的问题的。 - Muhammed Refaat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接