Android-我怎么可以转换android.net.Uri对象java.net.URI中的对象? [英] Android- how can I convert android.net.Uri object to java.net.URI object?

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

问题描述

感谢您的帮助。

我想获得一个FileInputStream对象,用户从图片库中选择一个图像。这是android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI返回android的URI

内容://媒体/外部/图片/媒体/ 3

当我试图从该对象​​建立一个java URI对象,我得到异常的说明预计文件方案,URI一个IllegalArgumentException:内容://媒体/外部/图片/媒体/ 3 而android的URI显示计划为内容

从未发现原来的问题的解决方案。但是,如果你想要在图片库的图像的字节流,这片code能做到这一点。

 位图位= Media.getBitmap(getContentResolver(),imageUri);
ByteArrayOutputStream字节=新ByteArrayOutputStream();
bitmap.com preSS(Bitmap.Com pressFormat.JPEG,40个字节);
ByteArrayInputStream进行的FileInputStream =新ByteArrayInputStream的(bytes.toByteArray());
 

解决方案

您可以在java URI的字符串基于构造的组合使用android的URI的toString方法

  android.net.URI AURI =新android.net.URI(什么都);
 java.net.URI中的树里=新java.net.URI中(auri.toString());
 

Android的URI | Java的URI

Thanks for any help.

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

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());

解决方案

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天全站免登陆