Android-如何将 android.net.Uri 对象转换为 java.net.URI 对象? [英] Android- how can I convert android.net.Uri object to java.net.URI object?

查看:23
本文介绍了Android-如何将 android.net.Uri 对象转换为 java.net.URI 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户从图片库中选择的图像上获取 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<使用androidUritoString方法基于/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 URI |Java URI

这篇关于Android-如何将 android.net.Uri 对象转换为 java.net.URI 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆