Search code examples
androidimageviewandroid-imageviewandroid-file

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?


Solution

  • 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" />