我已经尝试了我在 stackoverflow 上找到的所有解决方案来解决这个问题,但都是徒劳的。运行以下代码时,mAlbumPhotoUri 为“/mnt/sdcard/photo/1342147146535.jpg”,Uri 类型。 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);
最佳答案
将选项 inJustDecodeBounds
添加到您的 BitmapFactory
中就是这样;它只解码 Bitmap
的大小并将该数据加载回选项对象的 outHeight
和 outWidth
。它不会解码实际的 Bitmap
并将其返回给您。
如果你想真正获得Bitmap
本身,删除那个选项并再次调用decodeFile()
。
关于android - decodeFile 为现有文件返回空位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11473819/