What I'm trying to do is pretty simple: Pick an image from my phone (anywhere) and convert it to a File
. My final goal is to use this upload this file into my DB (a thing that I already succeed with a picture that I took from the camera).
My situation is: when I'm trying to create my file from the path given by the Uri. It doesn't work. I have read from some topics on SO and some other websites that sometimes it's impossible to create a file from the Uri's path like on these websites :
https://freakycoder.com/android-notes-73-how-to-get-real-path-from-uri-2f78320987f5
https://www.dev2qa.com/how-to-get-real-file-path-from-android-uri/
Also tried that way : Android - Get file with path
but still doesn't work.
My current code : How I'm picking up the picture :
public void exportPicture() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, READ_REQUEST_CODE);
}
how my Uri is filling :
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (data != null) {
photoURI = data.getData();
Log.d("URI", "Uri:" + photoURI.toString());
}
}
}
how I'm trying to create the file :
if (photoURI != null) {
File file = new File (photoURI.getPath());
}
I got the following error :
java.io.FileNotFoundException: /external/file/186142 (No such file or directory)
I'm under Oreo.
EDIT : Answer found here : stackoverflow.com/a/52095352