Skip to content

Commit f9b3954

Browse files
authored
[image_picker] Fix android-16 not picking up file (#11320)
## Description Fixes an issue where `image_picker` returns a null file after selecting an image from the gallery on Android 16+, caused by incorrect handling of content URIs in the Android implementation. ## Changes - Updated `ImagePickerUtils.java` to handle Android 16+ behavior correctly - Fixed file picking logic ## Tests - Added unit tests for `effectiveUsePhotoPicker` to verify behavior on Android 16 (API 36) - Ensured correct behavior across API levels ## Issue Fixes flutter/flutter#182071 Fixes flutter/flutter#184826 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 1efac12 commit f9b3954

7 files changed

Lines changed: 76 additions & 12 deletions

File tree

‎packages/image_picker/image_picker_android/CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.8.13+22
2+
3+
* Fixes gallery image/video selection on Android API 36+ returning no paths when
4+
`useAndroidPhotoPicker` was false. The plugin now uses the Android Photo Picker on API 36 and
5+
above regardless of that flag.
6+
17
## 0.8.13+21
28

39
* Updates pigeon dev_dependency to ^27.3.2 for analyzer 14 compatibility.

‎packages/image_picker/image_picker_android/README.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ should add it to your `pubspec.yaml` as usual.
1515

1616
## Photo Picker
1717

18-
On Android 13 and above this packages uses the Android Photo Picker.
18+
On Android 16 and above, gallery image, video, and mixed-media picks always
19+
use the Android Photo Picker. `ImagePickerAndroid.useAndroidPhotoPicker` has
20+
no effect on those versions.
1921

20-
On Android 12 and below this package has optional Android Photo Picker functionality.
21-
22-
To use this feature, add the following code to your app before calling any `image_picker` APIs:
22+
On Android 15 and below, this package has optional Android Photo Picker
23+
functionality. To enable it, add the following code to your app before calling
24+
any `image_picker` APIs:
2325

2426
<?code-excerpt "main.dart (photo-picker-example)"?>
2527
```dart

‎packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerDelegate.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public void chooseMediaFromGallery(
306306

307307
private void launchPickMediaFromGalleryIntent(GeneralOptions generalOptions) {
308308
Intent pickMediaIntent;
309-
if (generalOptions.getUsePhotoPicker()) {
309+
if (ImagePickerUtils.effectiveUsePhotoPicker(generalOptions.getUsePhotoPicker())) {
310310
if (generalOptions.getAllowMultiple()) {
311311
int limit = ImagePickerUtils.getLimitFromOption(generalOptions);
312312

@@ -355,7 +355,7 @@ public void chooseVideoFromGallery(
355355

356356
private void launchPickVideoFromGalleryIntent(Boolean usePhotoPicker) {
357357
Intent pickVideoIntent;
358-
if (usePhotoPicker) {
358+
if (ImagePickerUtils.effectiveUsePhotoPicker(usePhotoPicker)) {
359359
pickVideoIntent =
360360
new ActivityResultContracts.PickVisualMedia()
361361
.createIntent(
@@ -464,7 +464,7 @@ public void chooseMultiImageFromGallery(
464464

465465
private void launchPickImageFromGalleryIntent(Boolean usePhotoPicker) {
466466
Intent pickImageIntent;
467-
if (usePhotoPicker) {
467+
if (ImagePickerUtils.effectiveUsePhotoPicker(usePhotoPicker)) {
468468
pickImageIntent =
469469
new ActivityResultContracts.PickVisualMedia()
470470
.createIntent(
@@ -481,7 +481,7 @@ private void launchPickImageFromGalleryIntent(Boolean usePhotoPicker) {
481481

482482
private void launchMultiPickImageFromGalleryIntent(Boolean usePhotoPicker, int limit) {
483483
Intent pickMultiImageIntent;
484-
if (usePhotoPicker) {
484+
if (ImagePickerUtils.effectiveUsePhotoPicker(usePhotoPicker)) {
485485
pickMultiImageIntent =
486486
new ActivityResultContracts.PickMultipleVisualMedia(limit)
487487
.createIntent(
@@ -516,7 +516,7 @@ public void chooseMultiVideoFromGallery(
516516

517517
private void launchMultiPickVideoFromGalleryIntent(Boolean usePhotoPicker, int limit) {
518518
Intent pickMultiVideoIntent;
519-
if (usePhotoPicker) {
519+
if (ImagePickerUtils.effectiveUsePhotoPicker(usePhotoPicker)) {
520520
pickMultiVideoIntent =
521521
new ActivityResultContracts.PickMultipleVisualMedia(limit)
522522
.createIntent(

‎packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerUtils.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.Arrays;
1616

1717
final class ImagePickerUtils {
18+
private static final int API_LEVEL_36 = 36;
19+
1820
/** returns true, if permission present in manifest, otherwise false */
1921
private static boolean isPermissionPresentInManifest(Context context, String permissionName) {
2022
try {
@@ -86,4 +88,22 @@ static int getLimitFromOption(GeneralOptions generalOptions) {
8688

8789
return effectiveLimit;
8890
}
91+
92+
/**
93+
* Returns whether gallery/media selection should use {@link
94+
* androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia} (Android Photo
95+
* Picker) instead of {@link android.content.Intent#ACTION_GET_CONTENT}.
96+
*
97+
* <p>On Android API 36+, {@code ACTION_GET_CONTENT} for images may be handled by the system photo
98+
* picker's {@code PhotopickerGetContentActivity}. That path combined with {@code
99+
* startActivityForResult} can return {@link android.app.Activity#RESULT_OK} without {@link
100+
* android.content.Intent#getData()} or usable {@link android.content.ClipData}, so the plugin
101+
* would complete with no paths. The {@code PickVisualMedia} contract uses the Activity Result API
102+
* and receives URIs reliably.
103+
*
104+
* <p>See <a href="https://github.com/flutter/flutter/issues/182071">flutter/flutter#182071</a>.
105+
*/
106+
static boolean effectiveUsePhotoPicker(boolean usePhotoPickerFromDart) {
107+
return Build.VERSION.SDK_INT >= API_LEVEL_36 || usePhotoPickerFromDart;
108+
}
89109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.imagepicker;
6+
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.robolectric.RobolectricTestRunner;
13+
import org.robolectric.annotation.Config;
14+
15+
@RunWith(RobolectricTestRunner.class)
16+
public class ImagePickerUtilsTest {
17+
18+
@Test
19+
@Config(sdk = 35)
20+
public void effectiveUsePhotoPicker_belowApi36_usesDartPreference() {
21+
assertFalse(ImagePickerUtils.effectiveUsePhotoPicker(false));
22+
assertTrue(ImagePickerUtils.effectiveUsePhotoPicker(true));
23+
}
24+
25+
@Test
26+
@Config(sdk = 36)
27+
public void effectiveUsePhotoPicker_onApi36_alwaysUsesPhotoPicker() {
28+
assertTrue(ImagePickerUtils.effectiveUsePhotoPicker(false));
29+
assertTrue(ImagePickerUtils.effectiveUsePhotoPicker(true));
30+
}
31+
}

‎packages/image_picker/image_picker_android/lib/image_picker_android.dart‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ class ImagePickerAndroid extends ImagePickerPlatform {
1616

1717
final ImagePickerApi _hostApi;
1818

19-
/// Sets [ImagePickerAndroid] to use Android 13 Photo Picker.
19+
/// Whether to use the Android Photo Picker on Android versions less than 16.
2020
///
21-
/// Currently defaults to false, but the default is subject to change.
21+
/// On Android 16+ the Photo Picker is always used regardless of this setting.
22+
///
23+
/// On earlier Android versions, setting this to false uses the legacy
24+
/// `ACTION_GET_CONTENT` picker instead of the Photo Picker.
25+
///
26+
/// Defaults to false.
2227
bool useAndroidPhotoPicker = false;
2328

2429
/// Registers this class as the default platform implementation.

‎packages/image_picker/image_picker_android/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: image_picker_android
22
description: Android implementation of the image_picker plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
5-
version: 0.8.13+21
5+
version: 0.8.13+22
66

77
environment:
88
sdk: ^3.12.0

0 commit comments

Comments
 (0)