【问题标题】:how to get an uri of an image resource in android如何在android中获取图像资源的uri
【发布时间】:2011-06-21 05:45:45
【问题描述】:

我需要打开一个intent来查看图片,如下:

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");不正确。

【问题讨论】:

    标签: android uri


    【解决方案1】:

    格式为:

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

    【讨论】:

    • Uri 路径 = Uri.parse("android.resource://net.londatiga.android.twitpic/" + R.drawable.icon);字符串 mpath = path.toString();执行此操作时出现 No such file or directory 错误
    • 当我尝试将图像作为电子邮件附件发送时,它正在发送文件但没有 .jpg 扩展名。所以图像在收件人电脑中无效。
    • @hemanthkumar 将此答案作为参考:stackoverflow.com/a/8476930/62921 他解释说,除了您的机器上没有可绘制的文件夹。这就是它使用 ID 并且您的 path.toString() 不起作用的原因。
    • 这对我不起作用。我得到 ActivityNotFoundException。 (android.content.ActivityNotFoundException:未找到处理 Intent 的 Activity { act=android.intent.action.VIEW dat=android.resource://com.mypackage.myapp/2130837582 }。LG E400 Android 2.3.6。
    • @LucioCrusca - 也许你应该指定意图类型 - 它是 jpeg 还是 png 图像?
    【解决方案2】:

    这是一个干净的解决方案,它通过 Builder 模式充分利用 android.net.Uri 类,避免重复组合和分解 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()
    }
    

    【讨论】:

    • 这解决了我的问题。我的 uri 形状就像“android.resource://com.craiovadata.guessthecountry/drawable/sky”
    【解决方案3】:
    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) );
        }
    

    【讨论】:

      【解决方案4】:

      对于那些有错误的人,您可能输入了错误的包名称。只需使用此方法。

      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 = "/";
      

      【讨论】:

      • 你真的为前斜线做了一个常量吗??
      • @SartherisStormhammer 是的 :),虽然我通常使用系统常量作为前斜线 ;)
      【解决方案5】:

      你想要图片资源的URI,R.drawable.goomb是图片资源。 Builder 函数创建您要求的 URI:

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

      【讨论】:

      • 所以你将类似“18”的东西传递给路径。这似乎不正确
      • @NickCardoso R.drawable.goomba 是我的资源文件夹中的可绘制对象。如果它的值为 18,这是否会使赋值不正确?
      • 当然可以。 developer.android.com/reference/android/net/…需要使用R文件中的ID来解析真实路径。
      • @NickCardoso 谢谢,但它没有,它工作得很好。正如您指出的文档所暗示的那样,该方案会处理该路径。
      【解决方案6】:

      根据上面的答案,我想分享一个关于如何为项目中的任何资源获取有效 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()
      

      【讨论】:

        【解决方案7】:

        根据上面的@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)这样使用

        【讨论】:

          猜你喜欢
          • 2014-05-22
          • 2011-09-14
          • 1970-01-01
          • 2014-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多