BitmapFactory.decodeFile returns null even image exists(即使图像存在,BitmapFactory.decodeFile 也会返回 null)
问题描述
保存文件:
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返回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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:即使图像存在,BitmapFactory.decodeFile 也会返回 null


- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01