如何在android中获取图像资源的uri

发布于 2024-10-16 03:35:20 字数 269 浏览 4 评论 0原文

我需要打开一个意图来查看图像,如下所示:

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

问题是 Uri uri = Uri.parse("@drawable/sample_1.jpg"); 不正确。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

北恋 2024-10-23 03:35:20

格式为:

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

[package] 是您的包名称

[res id] 是资源的 ID,例如 R.drawable.sample_1

将其拼接在一起,使用

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

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

执笏见 2024-10-23 03:35:20

这是一个干净的解决方案,充分利用 android.net.Uri类通过其 Builder 模式,避免 URI 字符串的重复组合和分解,而不依赖于硬编码字符串或有关 URI 语法的临时想法。

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

使用 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()
}

在 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()
        }
    }
}

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()
        }
    }
}
何以畏孤独 2024-10-23 03:35:20
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) );
    }
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) );
    }
心头的小情儿 2024-10-23 03:35:20

对于那些有错误的人,您可能输入了错误的包名称。只要用这个方法就可以了。

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

在哪里

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

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 = "/";
墨小墨 2024-10-23 03:35:20

基于上面的答案,我想分享一个关于如何为项目中的任何资源获取有效 Uri 的 kotlin 示例。我认为这是最好的解决方案,因为您不必在代码中输入任何字符串,也不必冒输入错误的风险。

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

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()
情痴 2024-10-23 03:35:20

您需要图像资源的 URI,而 R.drawable.goomb 是图像资源。 Builder 函数创建您要求的 URI:

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

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();
尐籹人 2024-10-23 03:35:20

基于上面的 @xnagyg 答案,我做了一个方便的扩展,希望对其他人也有用,

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

可以像 context.resources.getRawUri(R.raw.rawid) 一样使用

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)

尝蛊 2024-10-23 03:35:20
public static String getURIForResource (int resourceId) {
        
        return Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" +resourceId).toString();
    }
public static String getURIForResource (int resourceId) {
        
        return Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" +resourceId).toString();
    }
但可醉心 2024-10-23 03:35:20

要获取 Android 应用程序中可绘制资源的内容 URI,您需要使用 android.resource 方案构造一个 URI。

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

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)
    }
}
谁的新欢旧爱 2024-10-23 03:35:20

如果可绘制对象是矢量或者您生成的图像资源包含 XML 以及所有其他 PNG 文件,您将不会获得 Uri。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文