Convert file: Uri to File in Android

hpique picture hpique · Jun 4, 2010 · Viewed 413.4k times · Source

What's the easiest way to convert from a file: android.net.Uri to a File in Android?

Tried the following but it doesn't work:

 final File file = new File(Environment.getExternalStorageDirectory(), "read.me");
 Uri uri = Uri.fromFile(file);
 File auxFile = new File(uri.toString());
 assertEquals(file.getAbsolutePath(), auxFile.getAbsolutePath());

Answer

Adil Hussain picture Adil Hussain · Dec 3, 2011

What you want is...

new File(uri.getPath());

... and not...

new File(uri.toString());

Note: uri.toString() returns a String in the format: "file:///mnt/sdcard/myPicture.jpg", whereas uri.getPath() returns a String in the format: "/mnt/sdcard/myPicture.jpg".