Issue
What's the easiest way to convert from an android.net.Uri
object which holds a file:
type to a 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());
Solution
What you want is...
new File(uri.getPath());
... and not...
new File(uri.toString());
Notes
- For an
android.net.Uri
object which is nameduri
and created exactly as in the question,uri.toString()
returns aString
in the format"file:///mnt/sdcard/myPicture.jpg"
, whereasuri.getPath()
returns aString
in the format"/mnt/sdcard/myPicture.jpg"
. - I understand that there are nuances to file storage in Android. My intention in this answer is to answer exactly what the questioner asked and not to get into the nuances.
Answered By - Adil Hussain
Answer Checked By - Mildred Charles (JavaFixing Admin)