What is the easiest way to convert from an android.net.Uri object which holds a file:
type to a java.io.File object in Android?
I tried the following but it doesn't work:
File file = new File(Environment.getExternalStorageDirectory(), "read.me");
Uri uri = Uri.fromFile(file);
File auxFile = new File(uri.toString());
assertEquals(file.getAbsolutePath(), auxFile.getAbsolutePath());
What you want is...
new File(uri.getPath());
... and not...
new File(uri.toString());
android.net.Uri
object which is named uri
and created exactly as in the question, 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"
.