I recently fixed a known bug in my app that occurs on some devices; when the user takes a photo from an intent launched from my app, in the onActivityResult
Uri uri = intent.getData();
returns null. I managed to fix that in the suggested manner. However I get the exact same issue when the user needs to Select a picture from his phone's photo gallery. The same intent.getData() == null
.
Starting the intent:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
R.id.SELECT_IMAGE_ACTIVITY_REQUEST_CODE);
onActivityResult:
Uri selectedImage = data.getData();
String[] filePathColumn =
{MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
dataHasChanged(ACTION_PICTURE, filePath);
PS: Strangely enough if I start the intent like shown below I can get the intent.getData()
but only if I use the "Gallery" app on my samsung s4 and not the GooglePhotos app.
Intent pickImageIntent = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (pickImageIntent.resolveActivity(getPackageManager()) != null)
startActivityForResult(pickImageIntent, R.id.SELECT_IMAGE_ACTIVITY_REQUEST_CODE);
I am unsure on how to proceed. I find the entire Android intents affair very confusing sometimes.