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

ste*_*ter 11 android bitmap fileoutputstream imageview

保存文件:

FileOutputStream fo = null; 
try { 
        fo = this.openFileOutput("test.png", Context.MODE_WORLD_READABLE); 
} catch (FileNotFoundException e) { 
        e.printStackTrace(); 
} 
bitmap.compress(CompressFormat.PNG, 100, fo)
Run Code Online (Sandbox Code Playgroud)

加载文件:

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

最后一行给出了空指针异常,为什么BitmapFactory.decodeFile返回null?我可以验证文件是否正确保存,因为我可以使用adb将其拉出来并看到png正确显示.

Com*_*are 20

如果NullPointerException是直接在这一行:

i.setImageBitmap(BMAP);

然后你的问题inull.鉴于您正在调用setImageBitmap(),我猜这i是一个ImageView- 确保您的findViewById()调用正常.

此外,您应该使用以下内容来获得fname:

String fname = new File(getFilesDir(),"test.png").getAbsolutePath();