how to get an uri of an image resource in android

user602874 picture user602874 · Feb 4, 2011 · Viewed 89.1k times · Source

I need to open an intent to view an image as follows:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("@drawable/sample_1.jpg");
intent.setData(uri);
startActivity(intent);

The problem is that Uri uri = Uri.parse("@drawable/sample_1.jpg"); is incorrect.

Answer

Axarydax picture Axarydax · Feb 4, 2011

The format is:

"android.resource://[package]/[res id]"

[package] is your package name

[res id] is value of the resource ID, e.g. R.drawable.sample_1

to stitch it together, use

Uri path = Uri.parse("android.resource://your.package.name/" + R.drawable.sample_1);