Skip to content

Commit

Permalink
[image_picker_android] - will fix crash on Android 12+ devices (#6691)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mairramer committed May 10, 2024
1 parent 1ab2f5b commit f18b895
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker_android/CHANGELOG.md
@@ -1,3 +1,7 @@
## 0.8.12

* Fixes app crashes on Android 12+ caused by selecting images with size 0.

## 0.8.11

* Updates documentation to note that Android Photo Picker use is not optional on Android 13+.
Expand Down
Expand Up @@ -404,7 +404,7 @@ private void launchTakeVideoWithCameraIntent() {
} catch (ActivityNotFoundException e) {
try {
// If we can't delete the file again here, there's not really anything we can do about it.
//noinspection ResultOfMethodCallIgnored
// noinspection ResultOfMethodCallIgnored
videoFile.delete();
} catch (SecurityException exception) {
exception.printStackTrace();
Expand Down Expand Up @@ -515,7 +515,7 @@ private void launchTakeImageWithCameraIntent() {
} catch (ActivityNotFoundException e) {
try {
// If we can't delete the file again here, there's not really anything we can do about it.
//noinspection ResultOfMethodCallIgnored
// noinspection ResultOfMethodCallIgnored
imageFile.delete();
} catch (SecurityException exception) {
exception.printStackTrace();
Expand Down Expand Up @@ -692,7 +692,10 @@ private void handleChooseMediaResult(int resultCode, Intent intent) {
paths.add(new MediaPath(path, mimeType));
}
} else {
paths.add(new MediaPath(fileUtils.getPathFromUri(activity, intent.getData()), null));
Uri uri = intent.getData();
if (uri != null) {
paths.add(new MediaPath(fileUtils.getPathFromUri(activity, uri), null));
}
}
handleMediaResult(paths);
return;
Expand Down
Expand Up @@ -182,7 +182,6 @@ public void chooseMediaFromGallery_whenPendingResultExists_finishesWithAlreadyAc
@Test
@Config(sdk = 30)
public void chooseImageFromGallery_launchesChooseFromGalleryIntent() {

ImagePickerDelegate delegate = createDelegate();
delegate.chooseImageFromGallery(DEFAULT_IMAGE_OPTIONS, false, mockResult);

Expand All @@ -194,7 +193,6 @@ public void chooseImageFromGallery_launchesChooseFromGalleryIntent() {
@Test
@Config(minSdk = 33)
public void chooseImageFromGallery_withPhotoPicker_launchesChooseFromGalleryIntent() {

ImagePickerDelegate delegate = createDelegate();
delegate.chooseImageFromGallery(DEFAULT_IMAGE_OPTIONS, true, mockResult);

Expand All @@ -206,7 +204,6 @@ public void chooseImageFromGallery_withPhotoPicker_launchesChooseFromGalleryInte
@Test
@Config(sdk = 30)
public void chooseMultiImageFromGallery_launchesChooseFromGalleryIntent() {

ImagePickerDelegate delegate = createDelegate();
delegate.chooseMultiImageFromGallery(
DEFAULT_IMAGE_OPTIONS, true, Integer.MAX_VALUE, mockResult);
Expand All @@ -220,7 +217,6 @@ public void chooseMultiImageFromGallery_launchesChooseFromGalleryIntent() {
@Test
@Config(minSdk = 33)
public void chooseMultiImageFromGallery_withPhotoPicker_launchesChooseFromGalleryIntent() {

ImagePickerDelegate delegate = createDelegate();
delegate.chooseMultiImageFromGallery(
DEFAULT_IMAGE_OPTIONS, false, Integer.MAX_VALUE, mockResult);
Expand All @@ -234,7 +230,6 @@ public void chooseMultiImageFromGallery_withPhotoPicker_launchesChooseFromGaller
@Test
@Config(sdk = 30)
public void chooseVideoFromGallery_launchesChooseFromGalleryIntent() {

ImagePickerDelegate delegate = createDelegate();
delegate.chooseVideoFromGallery(DEFAULT_VIDEO_OPTIONS, true, mockResult);

Expand All @@ -246,7 +241,6 @@ public void chooseVideoFromGallery_launchesChooseFromGalleryIntent() {
@Test
@Config(minSdk = 33)
public void chooseVideoFromGallery_withPhotoPicker_launchesChooseFromGalleryIntent() {

ImagePickerDelegate delegate = createDelegate();
delegate.chooseVideoFromGallery(DEFAULT_VIDEO_OPTIONS, true, mockResult);

Expand Down Expand Up @@ -820,6 +814,33 @@ public void onActivityResult_withUnknownRequest_returnsFalse() {
assertFalse(isHandled);
}

@Test
public void
onActivityResult_whenImagePickedFromGallery_finishesWithEmptyListIfIntentDataIsNull() {
setupMockClipDataNullUri();
when(mockIntent.getData()).thenReturn(null);
when(mockIntent.getClipData()).thenReturn(null);

Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate =
createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null);

delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_CHOOSE_MEDIA_FROM_GALLERY, Activity.RESULT_OK, mockIntent);

@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> pathListCapture = ArgumentCaptor.forClass(List.class);
verify(mockResult).success(pathListCapture.capture());
assertEquals(0, pathListCapture.getValue().size());
verifyNoMoreInteractions(mockResult);
}

private ImagePickerDelegate createDelegate() {
return new ImagePickerDelegate(
mockActivity,
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker_android/pubspec.yaml
Expand Up @@ -2,7 +2,7 @@ name: image_picker_android
description: Android implementation of the image_picker plugin.
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
version: 0.8.11
version: 0.8.12

environment:
sdk: ^3.3.0
Expand Down

0 comments on commit f18b895

Please sign in to comment.