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?

Android-Droid
  • 14,365
  • 41
  • 114
  • 185

5 Answers5

54

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();
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
karan
  • 3,319
  • 1
  • 35
  • 44
26

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

devgun
  • 1,003
  • 13
  • 33
Milan
  • 829
  • 8
  • 12
  • 1
    The only answer that works for me. How compatible is this composed URL across Android flavors from different providers? – Manuel May 15 '19 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 01 '21 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. – Ashish Jain Jan 04 '21 at 10:25
  • @AshishJain the above code has been tested for compatibility with regular images only not SVG – Milan Jan 06 '21 at 02: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 08 '21 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);
jmgomez
  • 742
  • 7
  • 13
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());
Ugnius Malūkas
  • 2,649
  • 7
  • 29
  • 42
ARN
  • 173
  • 2
  • 9
-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
        }
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243