我很困惑为什么会这样,因为我的小知识说它不应该发生。我有几个图像存储在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)
任何人都可以告诉它为什么会发生因为图像存在吗?提前致谢!
从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();