BitmapFactory.decodeFile 返回 null 即使图像存在 [英] BitmapFactory.decodeFile returns null even image exists
问题描述
保存文件:
FileOutputStream fo = null;
try {
fo = this.openFileOutput("test.png", Context.MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(CompressFormat.PNG, 100, fo)
加载文件:
String fname = this.getFilesDir().getAbsolutePath()+"/test.png";
Bitmap bMap = BitmapFactory.decodeFile(fname);
i.setImageBitmap(bMap);
最后一行给出了空指针异常,为什么 BitmapFactory.decodeFile 返回空?我可以验证文件是否正确保存,因为我可以使用 adb 拉它并查看 png 正确显示.
The last line gives a null pointer exception, why is BitmapFactory.decodeFile returning null? I can verify that the file is getting saved correctly as I can pull it using adb and see the png displaying properly.
推荐答案
如果 NullPointerException
直接在这一行:
If the NullPointerException
is directly on this line:
i.setImageBitmap(bMap);
i.setImageBitmap(bMap);
那么你的问题是 i
是 null
.鉴于您正在调用 setImageBitmap(),我猜测 i
是一个 ImageView
-- 确保您的 findViewById()
调用正常工作.
Then your problem is that i
is null
. Given that you're calling setImageBitmap(), I am guessing that i
is an ImageView
-- make sure your findViewById()
call is working.
此外,您应该使用以下内容来获取 fname
:
Also, you should use the following to get fname
:
String fname=new File(getFilesDir(), "test.png").getAbsolutePath();
String fname=new File(getFilesDir(), "test.png").getAbsolutePath();
这篇关于BitmapFactory.decodeFile 返回 null 即使图像存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!