Android-如何将 android.net.Uri 对象转换为 java.net.URI 对象? [英] Android- how can I convert android.net.Uri object to java.net.URI object?
问题描述
我试图在用户从图片库中选择的图像上获取 FileInputStream
对象.这是 android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
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
当我尝试从这个对象构造一个 java URI 对象时,我得到一个 IllegalArgumentException
和异常描述 URI 中的预期文件方案: content://media/external/images/media/3 而 android URI 将方案显示为 content
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());
推荐答案
你可以结合String<使用android
Uri
的toString
方法基于/code> 的 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-如何将 android.net.Uri 对象转换为 java.net.URI 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!