【问题标题】:Displaying Image by File Name按文件名显示图像
【发布时间】:2010-10-09 21:40:23
【问题描述】:

我需要通过指定文件名而不是使用资源来显示图像。例如 showImage("background.png") 而不是 showImage(R.drawable.background)

有人知道吗?

非常感谢。

【问题讨论】:

    标签: android imageview


    【解决方案1】:

    阅读此Android Image Tutorial。有一个使用 FileOutputStream 写入图像的示例,我确定也有 FileInputStream。这是another code example,它下载图像并将它们转换为解码后的输入流:

    bmImg = BitmapFactory.decodeStream(is);
    imView.setImageBitmap(bmImg);
    

    【讨论】:

      【解决方案2】:

      public static Bitmap decodeFile (String pathName, BitmapFactory.Options opts)

      这些选项不是强制性的,但如果您想显示大图像,您也应该提供它们。

      【讨论】:

        【解决方案3】:

        您可以使用下面的方法从资源文件夹中使用它的名称获取图像。

        public int getImage(String imageName) {
            return this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-21
          • 2017-05-28
          • 1970-01-01
          • 2022-01-05
          相关资源
          最近更新 更多