Sagar Chandra Chanda
Sagar Chandra Chanda

Reputation: 52

how to get image path from resource as a string in java in android

I want the image path from the drawable under resources as a string but it shows error . But I can't identify what the error is. I provide some code snippet.

listView = view.findViewById(R.id.listView);

ArrayList<Card> list = new ArrayList<>();

//here the error
String img = "drawable://" + R.drawable.tiger;

Note: when I placed image name (tiger) it gives me error

list.add(new Card("drawable://" + R.drawable.tiger, "Royal Bengal Tiger"));

Upvotes: 0

Views: 369

Answers (2)

tyczj
tyczj

Reputation: 73721

That's not how you get resources

what you want is ContextCompat.getDrawable(context, R.drawable.name);

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006564

i want the image path from the drawable under resources

A drawable resource is a file on your development machine. It is not a file on the device. There is no path to it on the device.

i provide some code snippet

There is no drawable scheme in Android. This also is not an image path.

If your Card requires a Uri, you can try android.resource as a scheme.

Upvotes: 1

Related Questions