即使图像存在,BitmapFactory.decodeFile 也会返回 null

编程入门 行业动态 更新时间:2024-10-25 04:16:46
本文介绍了即使图像存在,BitmapFactory.decodeFile 也会返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

保存文件:

FileOutputStream fo = null; try { fo = this.openFileOutput("test.png", Context.MODE_WORLD_READABLE); } catch (FileNotFoundException e) { e.printStackTrace(); } bitmappress(CompressFormat.PNG, 100, fo)

加载文件:

String fname = this.getFilesDir().getAbsolutePath()+"/test.png"; Bitmap bMap = BitmapFactory.decodeFile(fname); i.setImageBitmap(bMap);

最后一行给出了空指针异常,为什么BitmapFactory.decodeFile返回null?我可以验证文件是否正确保存,因为我可以使用 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直接在这一行:

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

本文发布于:2023-11-08 11:37:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也会   图像   decodeFile   BitmapFactory   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!