从文件路径显示图像视图? [英] Show Image View from file path?

查看:182
本文介绍了从文件路径显示图像视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要仅使用文件名来显示图像,而不是来自资源ID。

I need to show an image by using the file name only, not from the resource id.

ImageView imgView = new ImageView(this);
imgView.setBackgroundResource(R.drawable.img1);

我在drawable文件夹中有图像img1。我希望从文件中显示该图像。

I have the image img1 in the drawable folder. I wish to show that image from the file.

我该怎么做?

推荐答案

如果您的资源已经放在资源文件夹中,Labeeb是正确的,为什么需要使用路径设置图像,

Labeeb is right about why you need to set image using path if your resources are already laying inside the resource folder ,

只有当您的图像存储在SD卡中时才需要这种路径。

This kind of path is needed only when your images are stored in SD-Card .

并且尝试以下代码从存储在SD卡中的文件中设置位图图像。

File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}

并在清单文件中包含此权限:

And include this permission in the manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这篇关于从文件路径显示图像视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆