Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Image View from file path?

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

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

How can I do this?

like image 566
Alex Avatar asked Nov 15 '10 05:11

Alex


People also ask

How do I find the path of a picture File?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).


2 Answers

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

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

And try the below code to set Bitmap images from a file stored inside a SD-Card .

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" /> 
like image 146
Paresh Mayani Avatar answered Oct 18 '22 04:10

Paresh Mayani


I think you can use this

Bitmap bmImg = BitmapFactory.decodeFile("path of your img1"); imageView.setImageBitmap(bmImg); 
like image 28
Labeeb Panampullan Avatar answered Oct 18 '22 05:10

Labeeb Panampullan