【问题标题】:Get Bitmap from ImageView in Android L从 Android L 中的 ImageView 获取位图
【发布时间】:2015-01-08 01:18:42
【问题描述】:

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

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");

【问题讨论】:

  • 您的代码应该可以工作,确保您的图像视图具有可绘制对象,如果您使用 setBackground 设置图像,请改用 setBitmap

标签: java android android-layout bitmap imageview


【解决方案1】:

试试这个:

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

【讨论】:

  • 你能先解释一下你为什么要无效吗?这样做有充分的理由吗?
  • @Tom 如果你正在使用 Glide,那么试试这个 GlideBitmapDrawable drawable = (GlideBitmapDrawable) image.getDrawable();
【解决方案2】:

如果您只想要 ImageView 中的位图,以下代码可能适合您:-

Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();

尝试将图像放在所有可绘制质量文件夹中(drawable-hdpi/drawable-ldpi 等)

可能是您使用的模拟器或设备的密度不同,并且正在尝试从另一个文件夹中提取图像。

如果您在图像中使用 .png、.jpg 或 .gif 以外的扩展名,它可能无法识别其他扩展名类型。 http://developer.android.com/guide/topics/resources/drawable-resource.html

【讨论】:

  • 每次都返回 null ...
  • 上述文件夹中有图片吗?
  • 仅在可绘制文件夹中
  • 你能把同一张图片粘贴到所有文件夹里试试吗?
  • 在所有文件夹中仍然粘贴相同
【解决方案3】:

根据this answer,就这样吧:

imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();

【讨论】:

  • 这个一直都不能完美运行,通常会给出Canvas:尝试使用回收位图
  • 这个解决方案和这个答案(stackoverflow.com/questions/5566758/…) 对我有用:imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
  • getDrawingCache() 已弃用
【解决方案4】:

如果您尝试从 Glide 加载的图像中获取位图,那么这将对您有所帮助

 Drawable dr = ((ImageView) imView).getDrawable();
        Bitmap bmp =  ((GlideBitmapDrawable)dr.getCurrent()).getBitmap();

【讨论】:

    【解决方案5】:

    对于 Kotlin:

    只需编写此代码即可从 ImageView 中获取位图

    imageview.invalidate()
    val drawable = imageview.drawable
    val bitmap = drawable.toBitmap()
    

    【讨论】:

      【解决方案6】:

      对 ImagView 拍照并将其转换为字符串发送到服务器

          ImageView   ivImage1 = (ImageView ) findViewById(R.id.img_add1_send );
          getStringImage( ( ( BitmapDrawable ) ivImage1.getDrawable( ) ).getBitmap( ) ),
      
      
      
      public String getStringImage(Bitmap bm){
          ByteArrayOutputStream ba=new ByteArrayOutputStream(  );
          bm.compress( Bitmap.CompressFormat.PNG,90,ba );
          byte[] by=ba.toByteArray();
          String encod= Base64.encodeToString( by,Base64.DEFAULT );
          return encod;
      }
      

      【讨论】:

      • 抱歉跑题了。但是服务器(php)如何接收这个字符串......?是否需要任何编码器步骤?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多