我搞不懂为什么会发生这种事,因为我所知甚少,认为它不应该发生。我有几张图片存储在apk的android资源目录中,实际上存储在一些目录结构中,比如:
Assets --> aphotos --> launch50 ---> launch501.jpg现在,举个例子,我想通过编程将一张图片设置为imageview小部件,如下所示:
Uri uri = Uri.parse("file:///android_asset/"+context.getString(R.string.photoroot)+"/"+fldr+"/"+introphoto);
System.out.println("Image URI: "+uri.toString());
imageView.setImageURI(uri);其中"fldr“和"introphoto”来自数据库。执行此操作时,它会在“LogCat”中生成以下错误:-
11-22 19:23:46.689: W/ImageView(12990): Unable to open content: file:///android_asset/aphotos/launch50/launch501.jpg 11-22 19:23:46.689: W/ImageView(12990): java.io.FileNotFoundException: /android_asset/aphotos/launch50/launch501.jpg (No such file or directory) 11-22 19:23:46.689: W/ImageView(12990): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)有谁能说出为什么会发生这样的事情,因为有图像吗?提前感谢!
发布于 2012-11-22 23:06:11
从this question获取资产文件的URI显然不是很好。
我认为你需要用一个流来实现:
String path = context.getString(R.string.photoroot)+"/"+fldr+"/"+introphoto;
InputStream is = getAssets().open(path);
Bitmap bitmap = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(bitmap);
is.close();发布于 2021-12-07 10:15:43
我建议你使用下面的代码:
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("file:///android_asset/img/fruit/cherries.jpg").into(imageView);https://stackoverflow.com/questions/13515359
复制相似问题