BitmapFactory.de codeFILE返回null甚至存在图像 [英] BitmapFactory.decodeFile returns null even image exists

查看:160
本文介绍了BitmapFactory.de codeFILE返回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.de codeFILE返回null?我可以确认该文件得到正确保存,因为我可以把它利用亚行,看到了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异常直接是在这条线:

If the NullPointerException is directly on this line:

i.setImageBitmap(BMAP);

i.setImageBitmap(bMap);

那么你的问题是, I 。既然你打电话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:

字符串FNAME =新的文件(getFilesDir(),test.png),getAbsolutePath();

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

这篇关于BitmapFactory.de codeFILE返回null甚至存在图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆