【问题标题】:decodeFile returns null Bitmap for an existing filedecodeFile 为现有文件返回空位图
【发布时间】:2012-07-13 15:42:46
【问题描述】:

我已尝试在 stackoverflow 上找到的所有解决方案来解决此问题,但徒劳无功。在运行以下代码时,mAlbumPhotoUri 是 Uri 类型的“/mnt/sdcard/photo/1342147146535.jpg”。 file.exists() 表示该文件存在,但执行最后一行代码后,resultBitmap 为空。

我做错了什么?

File file = new File(mAlbumPhotoUri.toString());
if(file.exists()){
   Toast.makeText(this, "File exists in /mnt", Toast.LENGTH_LONG);}
else {
Toast.makeText(this, "File NOT exists in /mnt", Toast.LENGTH_LONG);}

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //only get the size of the bitmap                

if (resultPhotoBitmap != null) {
    resultPhotoBitmap.recycle();
    }
String fname=new File(mAlbumPhotoUri.toString()).getAbsolutePath();
resultPhotoBitmap = BitmapFactory.decodeFile(fname, options);

【问题讨论】:

  • 对不起,我忘了提.. 这是安卓系统。
  • 您是否尝试过在文件资源管理器中打开文件或以某种方式检查内容?

标签: android bitmapfactory


【解决方案1】:

将选项inJustDecodeBounds 添加到您的BitmapFactory就是这样;它只解码Bitmap 的大小并将该数据加载回选项对象的outHeightoutWidth。它不会解码实际的Bitmap 并将其返回给您。

如果您想真正获得Bitmap 本身,请删除该选项并再次调用decodeFile()

【讨论】:

    猜你喜欢
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多