Android-如何将 android.net.Uri 对象转换为 java.net.URI 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/559902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android- how can I convert android.net.Uri object to java.net.URI object?
提问by lostInTransit
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
我试图FileInputStream
在用户从图片库中选择的图像上获取一个对象。这是URI
返回的androidandroid.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/3whereas the android URI shows the scheme as content
当我尝试从这个对象构造一个 java URI 对象时,我得到一个IllegalArgumentException
异常描述URI 中的预期文件方案: content://media/external/images/media/3而 android URI 将方案显示为内容
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());
采纳答案by Brian Gianforcaro
You could use the toString
method of the android Uri
in combination of the String
based constructor of the Java URI
.
您可以toString
将 android的方法与 JavaUri
的String
基于构造函数结合使用URI
。
android.net.Uri auri = new android.net.Uri(what ever);
java.net.URI juri = new java.net.URI(auri.toString());
回答by Fedor
Found the correct way to open InputStream from content URI:
找到从内容 URI 打开 InputStream 的正确方法:
InputStream fileInputStream=yourContext.getContentResolver().openInputStream(uri);
That's all!
就这样!
回答by jgilrincon
There is a solution to your original question (convert Uri to URI):
您的原始问题有一个解决方案(将 Uri 转换为 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)
获取真实的文件路径(看这个代码:Get filename and path from URI from mediastore)
使用真实路径和构造函数获取 URI:URI(String uri)
If you need more details, look here:
如果您需要更多详细信息,请查看此处:
How to delete a video recorded using an Intent with ACTION_VIDEO_CAPTURE?
回答by Mixaz
I voted for jgilrincon's answer. I can't comment due to low reputation, and here goes some additional info - you can use FileHelper.javafrom Apache Cordova project, it has functions that you need for file handling from Uri strings, considering mediastore as well (and app assets folder)
我投票支持 jgilrincon 的答案。由于声誉低,我无法发表评论,这里有一些附加信息 - 您可以使用Apache Cordova 项目中的FileHelper.java,它具有从 Uri 字符串处理文件所需的功能,考虑到 mediastore(和应用程序资产文件夹) )
Particularly this method provides InputStream from Uri:
特别是这个方法提供了来自 Uri 的 InputStream:
public static InputStream getInputStreamFromUriString(String uriString, Activity cordova)
回答by Brian Gianforcaro
Since the String constructing doesn't work have you tried just constructing it your self?
由于 String 构造不起作用,您是否尝试过自己构造它?
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.
您可能还想仔细检查您是否从 Android URI 类中获取了有效数据。我的其他答案中列出的文档讨论了它如何几乎不进行错误检查。如果实际上有错误,该类只会吐出垃圾并且不会抛出任何异常。这很可能就是执行验证的 java 类抛出异常的原因。