user602874
user602874

Reputation: 1489

how to get an uri of an image resource in android

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.

Upvotes: 112

Views: 107435

Answers (10)

Abraham Mathew
Abraham Mathew

Reputation: 2146

You will not get a Uri, if the drawable is a vector or if the image asset you generated, includes an XML along with all the other PNG files.

https://github.com/bumptech/glide/issues/2137

Upvotes: 0

svg
svg

Reputation: 189

To get the content URI for a drawable resource in an Android application, you need to construct a URI using the android.resource scheme.

object ResourceUtils {
    /**
     * Get the content URI for a drawable resource.
     *
     * @param context  the context
     * @param resId    the resource ID of the drawable
     * @return the content URI
     */
    fun getDrawableUri(context: Context, resId: Int): Uri {
        return Uri.parse("android.resource://" + context.packageName + "/" + resId)
    }
}

Upvotes: 0

Khalid Lakhani
Khalid Lakhani

Reputation: 188

public static String getURIForResource (int resourceId) {
        
        return Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" +resourceId).toString();
    }

Upvotes: 0

Uli
Uli

Reputation: 3016

Here is a clean solution which fully leverages the android.net.Uri class via its Builder pattern, avoiding repeated composition and decomposition of the URI string, without relying on hard-coded strings or ad hoc ideas about URI syntax.

Resources resources = context.getResources();
Uri uri = new Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .authority(resources.getResourcePackageName(resourceId))
    .appendPath(resources.getResourceTypeName(resourceId))
    .appendPath(resources.getResourceEntryName(resourceId))
    .build();

Minimally more elegant with Kotlin:

fun Context.resourceUri(resourceId: Int): Uri = with(resources) {
    Uri.Builder()
        .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
        .authority(getResourcePackageName(resourceId))
        .appendPath(getResourceTypeName(resourceId))
        .appendPath(getResourceEntryName(resourceId))
        .build()
}

In Jetpack Compose:

@Composable
fun rememberResourceUri(resourceId: Int): Uri {
    val context = LocalContext.current

    return remember(resourceId) {
        with(context.resources) {
            Uri.Builder()
                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
                .authority(getResourcePackageName(resourceId))
                .appendPath(getResourceTypeName(resourceId))
                .appendPath(getResourceEntryName(resourceId))
                .build()
        }
    }
}

Upvotes: 99

Ebrahim Byagowi
Ebrahim Byagowi

Reputation: 11218

Based on @xnagyg answer above I've made a convenience extension which hopefully will be useful for others also,

fun Resources.getRawUri(@RawRes rawRes: Int) = "%s://%s/%s/%s".format(
    ContentResolver.SCHEME_ANDROID_RESOURCE, this.getResourcePackageName(rawRes),
    this.getResourceTypeName(rawRes), this.getResourceEntryName(rawRes)
)

which can be used like context.resources.getRawUri(R.raw.rawid)

Upvotes: 1

Sergio
Sergio

Reputation: 2473

Based on the answers above I want to share a kotlin example on how to get a valid Uri for any resource in your project. I think it's the best solution because you don't have to type any strings in your code and risk typing it wrongly.

    val resourceId = R.raw.scannerbeep // r.mipmap.yourmipmap; R.drawable.yourdrawable
    val uriBeepSound = Uri.Builder()
        .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
        .authority(resources.getResourcePackageName(resourceId))
        .appendPath(resources.getResourceTypeName(resourceId))
        .appendPath(resources.getResourceEntryName(resourceId))
        .build()

Upvotes: 5

roide
roide

Reputation: 127

You want the URI of the image resource, and R.drawable.goomb is an image resource. The Builder function creates the URI that you are asking for:

String resourceScheme = "res";
Uri uri = new Uri.Builder()
  .scheme(resourceScheme)
  .path(String.valueOf(intResourceId))
  .build();

Upvotes: 3

xnagyg
xnagyg

Reputation: 4922

public static Uri resourceToUri(Context context, int resID) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                context.getResources().getResourcePackageName(resID) + '/' +
                context.getResources().getResourceTypeName(resID) + '/' +
                context.getResources().getResourceEntryName(resID) );
    }

Upvotes: 59

sandalone
sandalone

Reputation: 41749

For those having error, you may be entering the wrong package name. Just use this method.

public static Uri resIdToUri(Context context, int resId) {
    return Uri.parse(Consts.ANDROID_RESOURCE + context.getPackageName()
                     + Consts.FORESLASH + resId);
}

Where

public static final String ANDROID_RESOURCE = "android.resource://";
public static final String FORESLASH = "/";

Upvotes: 11

Axarydax
Axarydax

Reputation: 16603

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);

Upvotes: 154

Related Questions