I have an app which allows to select photos with an external app. Then I take the path of the photo from the uri and use it for internal actions.
When user selects a photo with Google Photo, if the picture is locally stored then the next code works perfectly. But if the picture is in the cloud the result of cursor.getString(index) is null.
I've search for some info, but not sure about the solution
final String[] projection = { "_data" };
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow("_data");
return cursor.getString(index);
}
Thank you!
Not necessarily. There is no requirement for that
Uri
to respond with a_data
column to aquery()
. There is no requirement for the value it returns to be useful to you (e.g., a file on internal storage or removable storage that you cannot access).If you need the photo loaded into an
ImageView
, pass theUri
to an image-loading library, such as Picasso.If you need the bytes of the photo, use
openInputStream()
withContentResolver
to get anInputStream
on the content identified by theUri
. Please open and read from theInputStream
on a background thread.Finally and according to @CommonsWare answer and the previous post about this issue I solved getting the InputStream from the uri, coping into a new temporal file and passing the path to the function I need to use.
Here is the simplificated code: