【问题标题】:Get Bitmap attached to ImageView获取附加到 ImageView 的位图
【发布时间】:2012-01-08 13:09:45
【问题描述】:

给定

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

是否可以检索位图?

【问题讨论】:

  • 是的,有可能当您点击图片时,如果您需要此要求,请告诉我。

标签: android imageview android-bitmap


【解决方案1】:

对于那些正在寻找Kotlin 解决方案的人,可以从ImageView 获得Bitmap

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

【讨论】:

  • 我得到 AppCompatImageView 无法转换为 android.graphics.drawable.BitmapDrawable
【解决方案2】:

写下代码

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

【讨论】:

  • 我得到 AppCompatImageView 无法转换为 android.graphics.drawable.BitmapDrawable
【解决方案3】:

这段代码更好。

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(); -> 意味着从可绘制文件夹中获取图像? CMIIW ....@艾哈迈德
  • 没有。您可以使用此代码。Bitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.jellybean_statue);
【解决方案4】:

这将为您从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);

【讨论】:

  • 当它“不起作用”时,会发生什么?它是返回 null 还是抛出异常还是什么?
  • 它返回空值。有时我必须重新加载页面才能真正出现。
  • 给我一个空指针。 :( 在这一行:Bitmap bmap = Bitmap.createBitmap(mImageView.getDrawingCache());
  • drawingCache 在 Kotlin 中已弃用
【解决方案5】:

试试这个代码:

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

【讨论】:

  • 您能否描述对@Arslan 接受的答案的改进?
  • 你最好解释一下为什么你的答案解决了他的问题
【解决方案6】:

获取图像位图的其他方法是这样做:

Bitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.jellybean_statue);
imageView.setImageBitmap(imagenAndroid);

【讨论】:

    【解决方案7】:
    Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
    

    【讨论】:

    • 仔细检查您的image.getDrawable() 是否真的可以转换为BitmapDrawable(以避免IllegalCastExceptions)。例如,如果您在图像中使用图层,那么这个 sn-p 将略有不同:Bitmap bitmap = ((BitmapDrawable)((LayerDrawable)image.getDrawable()).getDrawable(0)).getBitmap();
    • 这将偶尔返回带有部分或全部黑色像素的位图。
    • 如果你在drawable/imageview上应用了,这不会返回原始位图也不会返回过滤后的位图。
    • 如果ImageView 中的图像是从URI 设置的,这是否有效? imageView.setImageUri()
    • @praneethkumar 它确实适用于我的场景。为这个很棒的答案点赞!
    猜你喜欢
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多