Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 即使存在图像,BitmapFactory.decodeFile也返回null_Android_Bitmap_Imageview_Fileoutputstream - Fatal编程技术网

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,因为我错误地引用了它。