Android 即使存在图像,BitmapFactory.decodeFile也返回null
保存文件:Android 即使存在图像,BitmapFactory.decodeFile也返回null,android,bitmap,imageview,fileoutputstream,Android,Bitmap,Imageview,Fileoutputstream,保存文件: 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 = t
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);
正在加载文件:
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);
最后一行给出了一个null指针异常,为什么BitmapFactory.decodeFile返回null?我可以验证文件是否正确保存,因为我可以使用adb拉取它,并看到png正确显示。如果
NullPointerException
直接位于此行:
i、 setImageBitmap(bMap)
那么您的问题是i
是null
。鉴于您正在调用setImageBitmap(),我猜I
是一个ImageView
——请确保您的findViewById()
调用正常
此外,还应使用以下命令获取fname
:
字符串fname=新文件(getFilesDir(),“test.png”).getAbsolutePath()
在DecodeFile方法中使用options参数时,请确保InJustDecodeBounds属性设置为false,否则它将始终返回null。当您只想解码文件,但不需要在应用程序/代码中进一步解码时,可以将此设置为true。这样就不需要分配额外的内存。请参阅以获取示例。是否已关闭文件输出流?“i”设置为什么,为什么它有一个单一字符的名称?是的,它是关闭的。我是一个图像视图,它被设置为null,因为我错误地引用了它。