Android- how can I convert android.net.Uri object to java.net.URI object?

爷,独闯天下 提交于 2019-12-17 07:17:06

问题


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

回答1:


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




回答2:


Found the correct way to open InputStream from content URI:

InputStream fileInputStream=yourContext.getContentResolver().openInputStream(uri);

That's all!




回答3:


There is a solution to your original question (convert Uri to URI):

  1. Get the real file path (look this code: Get filename and path from URI from mediastore)

  2. 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?




回答4:


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:

public static InputStream getInputStreamFromUriString(String uriString, Activity cordova)



回答5:


Since the String constructing doesn't work have you tried just constructing it your self?

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.



来源:https://stackoverflow.com/questions/559902/android-how-can-i-convert-android-net-uri-object-to-java-net-uri-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!