gpt4 book ai didi

android - BitmapFactory 无法解码流。致命的空指针错误

转载 作者:行者123 更新时间:2023-11-29 21:08:53 25 4
gpt4 key购买 nike

我有解码流的问题。我正在尝试将文件夹/MyApp/icons 中的图像放到 imageView 中。我有一个 fatal error

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 中?谢谢。 enter image description here

最佳答案

将您的文件夹放在项目的根目录中并不意味着它会将该文件夹放在您的 Android 设备的根目录中...
那么,对于您的情况,我建议您将文件保存在 assets 文件夹中,然后使用 AssetManager 加载文件。

例子如下:

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

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

@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);
}

关于android - BitmapFactory 无法解码流。致命的空指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23597545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com