How to convert Android Uri to Java URI

2019-06-01 16:14发布

问题:

I'm trying to figure out how to convert an Android Uri to a Java URI. What I'm trying to do is get a File, but as far as I can tell, I need to pass it a Java URI.

Here's my Uri that I'm attempting to convert:

content://media/external/images/media/100

And what I'm attempting to get at:

File mediaFile = new File(new URI("android.net.Uri"));

Where "android.net.Uri" is my Uri object

If there is a different/better way to get a java.io.File object from this content Uri, I'm open to suggestions as I've searched far and wide with no luck so far.

回答1:

Got it figured out, I was able to get the real filepath of the Uri and then create the File:

Uri uri = Uri.parse("content://media/external/images/media/47");

String[] projection = { MediaStore.Images.Media.DATA };
Cursor cur = managedQuery(uri, projection, null, null, null);
cur.moveToFirst();
String path = cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA));

mediaFile = new File(path);


标签: android uri