Android-如何将 android.net.Uri 对象转换为 java.net.URI 对象?

发布于 2024-07-13 23:50:55 字数 812 浏览 10 评论 0原文

我试图在用户从图片库中选择的图像上获取 FileInputStream 对象。 这是 android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI 返回的 android URI

content://media/external/images/media/3

当我尝试从此对象构造 java URI 对象时,我得到一个 IllegalArgumentException ,异常描述 URI 中的预期文件方案:content://media/external/images/media/3 而 android URI 将方案显示为 content

<强>更新: 从未找到原始问题的解决方案。 但是,如果您想要图片库中图像的字节流,这段代码就可以做到。

Bitmap bitmap = Media.getBitmap(getContentResolver(), imageUri);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
ByteArrayInputStream fileInputStream = new ByteArrayInputStream(bytes.toByteArray());

I am trying to get a FileInputStream object on an image that the user selects from the picture gallery. This is the android URI returned by android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI

content://media/external/images/media/3

When I try to construct a java URI object from this object, I get an IllegalArgumentException with the exception description Expected file scheme in URI: content://media/external/images/media/3 whereas the android URI shows the scheme as content

Update:
Never found a solution for the original question. But if you want the byte stream of an image in the pictures gallery, this piece of code will do that.

Bitmap bitmap = Media.getBitmap(getContentResolver(), imageUri);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
ByteArrayInputStream fileInputStream = new ByteArrayInputStream(bytes.toByteArray());

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

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

发布评论

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

评论(5

2024-07-20 23:50:55

您可以将 android UritoString 方法与 Java URI 的基于 String 的构造函数结合使用。

android.net.Uri auri = new android.net.Uri(what ever);
java.net.URI juri = new java.net.URI(auri.toString());

Android URI |
Java URI

You could use the toString method of the android Uri in combination of the String based constructor of the Java URI.

android.net.Uri auri = new android.net.Uri(what ever);
java.net.URI juri = new java.net.URI(auri.toString());

Android URI |
Java URI

不弃不离 2024-07-20 23:50:55

找到了从内容 URI 打开 InputStream 的正确方法:

InputStream fileInputStream=yourContext.getContentResolver().openInputStream(uri);

仅此而已!

Found the correct way to open InputStream from content URI:

InputStream fileInputStream=yourContext.getContentResolver().openInputStream(uri);

That's all!

记忆里有你的影子 2024-07-20 23:50:55

您原来的问题有一个解决方案(将 Uri 转换为 URI):

  1. 获取真实文件路径(查看此代码:从媒体存储中的 URI 获取文件名和路径

  2. 使用真实路径获取 URI和构造函数:URI(String uri)

如果您需要更多详细信息,请查看此处:

如何删除使用带有 ACTION_VIDEO_CAPTURE 的 Intent 录制的视频?

There is a solution to your original question (convert Uri to URI):

  1. Get the real file path (look this code: Get filename and path from URI from mediastore)

  2. Get the URI using the real path and the constructor: URI(String uri)

If you need more details, look here:

How to delete a video recorded using an Intent with ACTION_VIDEO_CAPTURE?

雅心素梦 2024-07-20 23:50:55

我投票支持 jgilrincon 的答案。 由于声誉较低,我无法发表评论,这里有一些附加信息 - 您可以使用 FileHelper.java 来自 Apache Cordova 项目,它具有从 Uri 字符串处理文件所需的功能,还考虑到媒体存储(和应用程序资产文件夹),

特别是这个方法从 Uri 提供 InputStream:

public static InputStream getInputStreamFromUriString(String uriString, Activity cordova)

I voted for jgilrincon's answer. I can't comment due to low reputation, and here goes some additional info - you can use FileHelper.java from Apache Cordova project, it has functions that you need for file handling from Uri strings, considering mediastore as well (and app assets folder)

Particularly this method provides InputStream from Uri:

public static InputStream getInputStreamFromUriString(String uriString, Activity cordova)
不再见 2024-07-20 23:50:55

由于字符串构造不起作用,您是否尝试过自己构造它?

android.net.URI auri = new android.net.URI(what ever);
java.net.URI juri = new java.net.URI(auri.getSchema(),
                                     auri.getSchemaSpecificPart(),
                                     auri.getFragment());

您可能还需要仔细检查是否从 Android URI 类中获取了有效数据。 我的其他答案中列出的文档讨论了它如何几乎不进行错误检查。 如果实际上存在错误,该类无论如何都会吐出垃圾并且不会抛出任何异常。 这很可能就是执行验证的 java 类抛出异常的原因。

Since the String constructing doesn't work have you tried just constructing it your self?

android.net.URI auri = new android.net.URI(what ever);
java.net.URI juri = new java.net.URI(auri.getSchema(),
                                     auri.getSchemaSpecificPart(),
                                     auri.getFragment());

You might also want to double check that your getting valid data out of Android URI class. The docs as listed in my other answer discuss how it does pretty much no error checking. If there is infact an error the class just spits out garbage anyway and doesn't throw any exceptions. Which could very likely be why the java class which does do validation is throwing an exception.

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