Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -629,24 +629,48 @@ public boolean onActivityResult(
return true;
}

private void handleChooseImageResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && data != null) {
Uri uri = data.getData();
// On several pre-Android 13 devices using Android Photo Picker, the Uri from getData() could be null.
if (uri == null) {
ClipData clipData = data.getClipData();
if (clipData != null && clipData.getItemCount() == 1) {
uri = clipData.getItemAt(0).getUri();
private ArrayList<MediaPath> getPathsFromIntent(Intent data, boolean includeMimeType) {
ArrayList<MediaPath> paths = new ArrayList<>();

Uri uri = data.getData();
// On several pre-Android 13 devices using Android Photo Picker, the Uri from getData() could
// be null.
if (uri == null) {
ClipData clipData = data.getClipData();

// If data.getData() and data.getClipData() are both null, we are in an error state. By
// convention we return null from here, and then finish with an error from the corresponding
// handler.
if (clipData == null) {
return null;
}

for (int i = 0; i < data.getClipData().getItemCount(); i++) {
uri = data.getClipData().getItemAt(i).getUri();
// Same error state as above.
if (uri == null) {
return null;
}
String path = fileUtils.getPathFromUri(activity, uri);
String mimeType = includeMimeType ? activity.getContentResolver().getType(uri) : null;
paths.add(new MediaPath(path, mimeType));
}
} else {
paths.add(new MediaPath(fileUtils.getPathFromUri(activity, uri), null));
}
return paths;
}

private void handleChooseImageResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && data != null) {
ArrayList<MediaPath> paths = getPathsFromIntent(data, false);
// If there's no valid Uri, return an error
if (uri == null) {
if (paths == null) {
finishWithError("no_valid_image_uri", "Cannot find the selected image.");
return;
}

String path = fileUtils.getPathFromUri(activity, uri);
handleImageResult(path, false);
handleMediaResult(paths);
return;
}

Expand Down Expand Up @@ -674,17 +698,13 @@ public MediaPath(@NonNull String path, @Nullable String mimeType) {

private void handleChooseMediaResult(int resultCode, Intent intent) {
if (resultCode == Activity.RESULT_OK && intent != null) {
ArrayList<MediaPath> paths = new ArrayList<>();
if (intent.getClipData() != null) {
for (int i = 0; i < intent.getClipData().getItemCount(); i++) {
Uri uri = intent.getClipData().getItemAt(i).getUri();
String path = fileUtils.getPathFromUri(activity, uri);
String mimeType = activity.getContentResolver().getType(uri);
paths.add(new MediaPath(path, mimeType));
}
} else {
paths.add(new MediaPath(fileUtils.getPathFromUri(activity, intent.getData()), null));
ArrayList<MediaPath> paths = getPathsFromIntent(intent, true);
// If there's no valid Uri, return an error
if (paths == null) {
finishWithError("no_valid_media_uri", "Cannot find the selected media.");
Copy link
Collaborator

@stuartmorgan-g stuartmorgan-g May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we would use the same error code for all of these to make handling easier (until we have structured errors) but since we already have several different code strings and we generally consider changing platform exception error codes breaking since we leak them out of the plugin API surface, I think we're stuck with different codes for now.

(Meaning: no change needed to the PR, just leaving this as a breadcrumb for us to consider in the future.)

return;
}

handleMediaResult(paths);
return;
}
Expand All @@ -695,17 +715,14 @@ private void handleChooseMediaResult(int resultCode, Intent intent) {

private void handleChooseMultiImageResult(int resultCode, Intent intent) {
if (resultCode == Activity.RESULT_OK && intent != null) {
ArrayList<MediaPath> paths = new ArrayList<>();
if (intent.getClipData() != null) {
for (int i = 0; i < intent.getClipData().getItemCount(); i++) {
paths.add(
new MediaPath(
fileUtils.getPathFromUri(activity, intent.getClipData().getItemAt(i).getUri()),
null));
}
} else {
paths.add(new MediaPath(fileUtils.getPathFromUri(activity, intent.getData()), null));
ArrayList<MediaPath> paths = getPathsFromIntent(intent, false);
// If there's no valid Uri, return an error
if (paths == null) {
finishWithError("missing_valid_image_uri", "Cannot find at least one " +
"of the selected images.");
return;
}

handleMediaResult(paths);
return;
}
Expand All @@ -716,22 +733,14 @@ private void handleChooseMultiImageResult(int resultCode, Intent intent) {

private void handleChooseVideoResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && data != null) {
Uri uri = data.getData();
// On several pre-Android 13 devices using Android Photo Picker, the Uri from getData() could be null.
if (uri == null) {
ClipData clipData = data.getClipData();
if (clipData != null && clipData.getItemCount() == 1) {
uri = clipData.getItemAt(0).getUri();
}
}
ArrayList<MediaPath> paths = getPathsFromIntent(data, false);
// If there's no valid Uri, return an error
if (uri == null) {
if (paths == null || paths.size() < 1) {
finishWithError("no_valid_video_uri", "Cannot find the selected video.");
return;
}

String path = fileUtils.getPathFromUri(activity, uri);
handleVideoResult(path);
handleVideoResult(paths.get(0).path);
return;
}

Expand Down