Android-如何将 android.net.Uri 对象转换为 java.net.URI 对象?
我试图在用户从图片库中选择的图像上获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将 android
Uri
的toString
方法与 JavaURI
的基于String
的构造函数结合使用。Android URI |
Java URI
You could use the
toString
method of the androidUri
in combination of theString
based constructor of the JavaURI
.Android URI |
Java URI
找到了从内容 URI 打开 InputStream 的正确方法:
仅此而已!
Found the correct way to open InputStream from content URI:
That's all!
您原来的问题有一个解决方案(将 Uri 转换为 URI):
获取真实文件路径(查看此代码:从媒体存储中的 URI 获取文件名和路径)
使用真实路径获取 URI和构造函数:URI(String uri)
如果您需要更多详细信息,请查看此处:
如何删除使用带有 ACTION_VIDEO_CAPTURE 的 Intent 录制的视频?
There is a solution to your original question (convert Uri to URI):
Get the real file path (look this code: Get filename and path from URI from mediastore)
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?
我投票支持 jgilrincon 的答案。 由于声誉较低,我无法发表评论,这里有一些附加信息 - 您可以使用 FileHelper.java 来自 Apache Cordova 项目,它具有从 Uri 字符串处理文件所需的功能,还考虑到媒体存储(和应用程序资产文件夹),
特别是这个方法从 Uri 提供 InputStream:
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:
由于字符串构造不起作用,您是否尝试过自己构造它?
您可能还需要仔细检查是否从 Android URI 类中获取了有效数据。 我的其他答案中列出的文档讨论了它如何几乎不进行错误检查。 如果实际上存在错误,该类无论如何都会吐出垃圾并且不会抛出任何异常。 这很可能就是执行验证的 java 类抛出异常的原因。
Since the String constructing doesn't work have you tried just constructing it your self?
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.