43

Is there any way that I can get the image path from drawable folder in android as String. I need this because I implement my own viewflow where I'm showing the images by using their path on sd card, but I want to show a default image when there is no image to show.

Any ideas how to do that?

3

5 Answers 5

55

These all are ways:

String imageUri = "drawable://" + R.drawable.image;

Other ways I tested

Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");

String path = path.toString();
String path = otherPath .toString();
2
  • I am getting file format not supported error with all of them. Jun 2, 2016 at 8:37
  • 3
    such pattern returns FileNotFound Exception Sep 22, 2016 at 16:32
27

based on the some of above replies i improvised it a bit

create this method and call it by passing your resource

Reusable Method

public String getURLForResource (int resourceId) {
    //use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
    return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
}

Sample call

getURLForResource(R.drawable.personIcon)

complete example of loading image

String imageUrl = getURLForResource(R.drawable.personIcon);
// Load image
 Glide.with(patientProfileImageView.getContext())
          .load(imageUrl)
          .into(patientProfileImageView);

you can move the function getURLForResource to a Util file and make it static so it can be reused

5
  • 1
    The only answer that works for me. How compatible is this composed URL across Android flavors from different providers?
    – Manuel
    May 15, 2019 at 15:57
  • @AshishJain, you need to share more information, like the Android version; whats the image type, where the is image placed error if any, etc..
    – Milan
    Jan 1, 2021 at 19:15
  • I am trying same code for Android all version like 8,9 and 10 and passing vector image (.xml) but it doesn't showing on my custom recyclerview. Jan 4, 2021 at 10:25
  • @AshishJain the above code has been tested for compatibility with regular images only not SVG
    – Milan
    Jan 6, 2021 at 2:53
  • 1
    you should use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() cause some people name the package different than the app id
    – R. A.
    Apr 8, 2021 at 19:38
3

If you are planning to get the image from its path, it's better to use Assets instead of trying to figure out the path of the drawable folder.

    InputStream stream = getAssets().open("image.png");
    Drawable d = Drawable.createFromStream(stream, null);
2

I think you cannot get it as String but you can get it as int by get resource id:

int resId = this.getResources().getIdentifier("imageNameHere", "drawable", this.getPackageName());
-6

First check whether the file exists in SDCard. If the file doesnot exists in SDcard then you can set image using setImageResource() methodand passing default image from drawable folder

Sample Code

File imageFile = new File(absolutepathOfImage);//absolutepathOfImage is absolute path of image including its name
        if(!imageFile.exists()){//file doesnot exist in SDCard

        imageview.setImageResource(R.drawable.defaultImage);//set default image from drawable folder
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.