gpt4 book ai didi

android - 在 android 中使用 uri 将源设置为 imageview 会产生错误

转载 作者:太空狗 更新时间:2023-10-29 15:14:56 25 4
gpt4 key购买 nike

我很困惑为什么会发生这种情况,因为我的一点知识表明它不应该发生。我有几个图像存储在 apk 的 android Assets 目录中,实际上在一些目录结构中,如:

    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获取 Assets 文件的 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();

关于android - 在 android 中使用 uri 将源设置为 imageview 会产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13515359/

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