【问题标题】:setting source to imageview with uri in android generates error在android中使用uri将源设置为imageview会产生错误
【发布时间】:2012-11-11 00:12:19
【问题描述】:

我很困惑为什么会发生这种情况,因为我的知识表明它不应该发生。我有几个图像存储在 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)

谁能告诉它为什么会发生,因为图像在那里? 提前致谢!

【问题讨论】:

    标签: android uri android-imageview android-assets


    【解决方案1】:

    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();
    

    【讨论】:

    • 问题是为什么不打开我实际上不想要的流就无法完成。图片有,setImageURI 方法有,为什么不工作?
    • 这对我有用:- link 感谢您的友好回复。
    【解决方案2】:

    我建议你使用这个代码:

    ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
    
    Glide.with(this).load("file:///android_asset/img/fruit/cherries.jpg").into(imageView);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多