-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[image_picker_android] Refactor getting of paths from intent to single helper #5009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
89a1f66
9e1ee7a
98e9d72
bbec656
ac1e9ae
b0c5477
b94ca57
44b9210
d9cecb5
7d44826
30ccbc3
c2d5995
d54eb54
d50b654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
gmackall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // 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; | ||
gmackall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| 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; | ||
| } | ||
|
|
||
|
|
@@ -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."); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -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); | ||
gmackall marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.