显示从文件路径的android图片浏览?路径、文件、图片、android

2023-09-12 21:30:38 作者:若离,必弃。

我需要使用文件名不仅没有从资源ID来显示图像。

I need to show image by using File name only not from the Resource id.

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

我在绘制文件夹中的图像IMG1。我想表明的图像从文件如何做到这一点。

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

推荐答案

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 .

和请尝试以下code,从存储在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" />