保存文件:
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
发布评论