【问题标题】:BitmapFactory unable to decode stream. Fatal nullpointer errorBitmapFactory 无法解码流。致命的空指针错误
【发布时间】:2014-05-11 20:27:36
【问题描述】:

我的解码流有问题。我正在尝试将 /MyApp/icons 文件夹中的图像放入 imageView。我有一个致命错误

05-11 22:21:22.319: E/BitmapFactory(7981): Unable to decode stream: java.io.FileNotFoundException: /MyWeather/icons/a04n.png: open failed: ENOENT (No such file or directory)

我尝试设置 "icons/.." 、 "/icons/.." 和许多其他选项,但没有人可以工作。

public class Notifications extends Activity{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification);

        ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
        Bundle extras = getIntent().getExtras();
        String ikona = extras.getString("icon");

        Bitmap bm = BitmapFactory.decodeFile("MyWeather/icons/a"+ikona+".png");
        imageView1.setImageBitmap(bm);

    }

}

谁能告诉我,如何从该文件夹中获取此图标并将其设置在我的 imageView 中? 谢谢。

【问题讨论】:

  • MyWeather 在 SD 卡中吗?
  • MyWeather是一个android项目,icons是这个项目中的一个文件夹。
  • 亲爱的,我不认为你可以阅读这样的文件。将您的图标放在assest 文件夹中,如下所示example
  • 非常感谢。这是工作;)

标签: android


【解决方案1】:

将您的文件夹放在项目的根目录中并不意味着它将将该文件夹放在您的 Android 设备的根目录中...
好吧,在你的情况下,我建议你将文件保存在 assets 文件夹中并使用 AssetManager 加载文件。

这里是示例:

将您的“图标”文件夹放入资产文件夹。

使用以下代码解码您的图像文件:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notification);

    ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
    Bundle extras = getIntent().getExtras();
    String ikona = extras.getString("icon");

    AssetManager assetManager = getAssets();
    Bitmap bm;
    try {
        InputStream is = assetManager.open("icons/a"+ikona+".png");
        bm = BitmapFactory.decodeStream(is);    
    } catch (IOException e) {
        e.printStackTrace();
        // put your exception handling code here.
    }
    imageView1.setImageBitmap(bm);
}

【讨论】:

  • /MyWeather/icons/a04n.png 例如。
  • 你确定吗?您的日志显示该文件不存在...或者您的文件可能存在权限问题,您是否在 adb shell 中尝试过 chmod o+x /MyWeather/icons/a04n.png?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
相关资源
最近更新 更多