How to convert Android Uri to Java URI

99封情书 提交于 2020-01-03 03:03:05

问题


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


来源:https://stackoverflow.com/questions/7171458/how-to-convert-android-uri-to-java-uri

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