I have an image processing app. In my code, I pass my service an ArrayList and the service does the rest. Now I want to extend the functionality of my app and let users be able to go to the gallery, pick one picture and, using the share button, send it to my app to be processed. I want to reuse the most code as possible, so I decided that a good way to go would be converting those URIs returned by the send action to actual file paths. My solution works as expected with QuickPic, but not with Google Photos. My code is as follows:
//MainFragment.onCreateView()
Intent intent = getActivity().getIntent();
if(Intent.ACTION_SEND.equals(intent.getAction()) {
handleSingleImage(intent);
}
private void handleSingleImage(Intent intent) {
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
Log.d("MYURISTRING", uri.toString());
ArrayList<String> selectedPaths = new ArrayList<String>();
String path = Utils.getRealPathFromURI(getActivity(), uri);
selectedPaths.add(path);
Utils.startProcessPhotosService(getActivity(), MainFragment.this, selectedPaths);
}
//Utils
public static String getRealPathFromURI(Context context, Uri contentUri) {
/*String[] proj = {MediaStore.Images.Media.DATA};
CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(columnIndex);*/
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(contentUri, null, null, null, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
If I test this with a photo from QuickPic app, everything works as expected, and the URI on the log is as follows:
content://media/external/images/media/135695
But if I test this with Google Photos, my app crashes, and the URI is as follows:
content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F135669/ACTUAL
How can I do to support both styles of URI (and, possibly, more than these)? Thank you