Skip to content

Commit

Permalink
[image_picker] moves Android 13 image picker to options (#3370)
Browse files Browse the repository at this point in the history
[image_picker] moves Android 13 image picker to options
  • Loading branch information
tarrinneal committed Mar 9, 2023
1 parent 88ca9e2 commit 3814ec4
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/image_picker/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.8.7

* Adds `usePhotoPickerAndroid` options.
* Aligns Dart and Flutter SDK constraints.

## 0.8.6+4
Expand Down
5 changes: 5 additions & 0 deletions packages/image_picker/image_picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ case. Please refer to the
[example app](https://pub.dev/packages/image_picker/example) for a more
complete example of handling this flow.

### Android Photo Picker

This package has optional [Android Photo Picker](https://developer.android.com/training/data-storage/shared/photopicker) functionality.
[Learn how to use it](https://pub.dev/packages/image_picker_android).

## Migrating to 0.8.2+

Starting with version **0.8.2** of the image_picker plugin, new methods have been added for picking files that return `XFile` instances (from the [cross_file](https://pub.dev/packages/cross_file) package) rather than the plugin's own `PickedFile` instances. While the previous methods still exist, it is already recommended to start migrating over to their new equivalents. Eventually, `PickedFile` and the methods that return instances of it will be deprecated and removed.
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for selecting images from the Android and iOS image
library, and taking new pictures with the camera.
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
version: 0.8.6+4
version: 0.8.7

environment:
sdk: ">=2.17.0 <3.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.6

* Adds `usePhotoPickerAndroid` options.

## 0.8.5+10

* Clarifies explanation of endorsement in README.
Expand Down
20 changes: 20 additions & 0 deletions packages/image_picker/image_picker_android/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<?code-excerpt path-base="excerpts/packages/image_picker_example"?>

# image\_picker\_android

The Android implementation of [`image_picker`][1].
Expand All @@ -11,5 +13,23 @@ so you do not need to add it to your `pubspec.yaml`.
However, if you `import` this package to use any of its APIs directly, you
should add it to your `pubspec.yaml` as usual.

## Photo Picker

This package has optional Android Photo Picker functionality.

To use this feature, add the following code to your app before calling any `image_picker` APIs:

<?code-excerpt "main.dart (photo-picker-example)"?>
```dart
import 'package:image_picker_android/image_picker_android.dart';
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
// ···
final ImagePickerPlatform imagePickerImplementation =
ImagePickerPlatform.instance;
if (imagePickerImplementation is ImagePickerAndroid) {
imagePickerImplementation.useAndroidPhotoPicker = true;
}
```

[1]: https://pub.dev/packages/image_picker
[2]: https://flutter.dev/docs/development/packages-and-plugins/developing-packages#endorsed-federated-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,18 @@ public void chooseVideoFromGallery(MethodCall methodCall, MethodChannel.Result r
return;
}

launchPickVideoFromGalleryIntent();
Boolean usePhotoPicker = methodCall.argument("useAndroidPhotoPicker");

if (usePhotoPicker == null) {
usePhotoPicker = false;
}

launchPickVideoFromGalleryIntent(usePhotoPicker);
}

private void launchPickVideoFromGalleryIntent() {
private void launchPickVideoFromGalleryIntent(Boolean useAndroidPhotoPicker) {
Intent pickVideoIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (useAndroidPhotoPicker && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
pickVideoIntent =
new ActivityResultContracts.PickVisualMedia()
.createIntent(
Expand Down Expand Up @@ -326,7 +332,13 @@ public void chooseImageFromGallery(MethodCall methodCall, MethodChannel.Result r
return;
}

launchPickImageFromGalleryIntent();
Boolean usePhotoPicker = methodCall.argument("useAndroidPhotoPicker");

if (usePhotoPicker == null) {
usePhotoPicker = false;
}

launchPickImageFromGalleryIntent(usePhotoPicker);
}

public void chooseMultiImageFromGallery(MethodCall methodCall, MethodChannel.Result result) {
Expand All @@ -335,12 +347,18 @@ public void chooseMultiImageFromGallery(MethodCall methodCall, MethodChannel.Res
return;
}

launchMultiPickImageFromGalleryIntent();
Boolean usePhotoPicker = methodCall.argument("useAndroidPhotoPicker");

if (usePhotoPicker == null) {
usePhotoPicker = false;
}

launchMultiPickImageFromGalleryIntent(usePhotoPicker);
}

private void launchPickImageFromGalleryIntent() {
private void launchPickImageFromGalleryIntent(Boolean useAndroidPhotoPicker) {
Intent pickImageIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (useAndroidPhotoPicker && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
pickImageIntent =
new ActivityResultContracts.PickVisualMedia()
.createIntent(
Expand All @@ -356,9 +374,9 @@ private void launchPickImageFromGalleryIntent() {
activity.startActivityForResult(pickImageIntent, REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY);
}

private void launchMultiPickImageFromGalleryIntent() {
private void launchMultiPickImageFromGalleryIntent(Boolean useAndroidPhotoPicker) {
Intent pickMultiImageIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (useAndroidPhotoPicker && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
pickMultiImageIntent =
new ActivityResultContracts.PickMultipleVisualMedia()
.createIntent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public void chooseMultiImageFromGallery_WhenPendingResultExists_FinishesWithAlre
chooseImageFromGallery_WithPhotoPicker_WhenHasExternalStoragePermission_LaunchesChooseFromGalleryIntent() {
when(mockPermissionManager.isPermissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE))
.thenReturn(true);
when(mockMethodCall.argument("useAndroidPhotoPicker")).thenReturn(true);

ImagePickerDelegate delegate = createDelegate();
delegate.chooseImageFromGallery(mockMethodCall, mockResult);
Expand All @@ -177,6 +178,7 @@ public void chooseMultiImageFromGallery_WhenPendingResultExists_FinishesWithAlre
chooseMultiImageFromGallery_WhenHasExternalStoragePermission_LaunchesChooseFromGalleryIntent() {
when(mockPermissionManager.isPermissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE))
.thenReturn(true);
when(mockMethodCall.argument("useAndroidPhotoPicker")).thenReturn(true);

ImagePickerDelegate delegate = createDelegate();
delegate.chooseMultiImageFromGallery(mockMethodCall, mockResult);
Expand All @@ -193,6 +195,7 @@ public void chooseMultiImageFromGallery_WhenPendingResultExists_FinishesWithAlre
chooseMultiImageFromGallery_WithPhotoPicker_WhenHasExternalStoragePermission_LaunchesChooseFromGalleryIntent() {
when(mockPermissionManager.isPermissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE))
.thenReturn(true);
when(mockMethodCall.argument("useAndroidPhotoPicker")).thenReturn(true);

ImagePickerDelegate delegate = createDelegate();
delegate.chooseMultiImageFromGallery(mockMethodCall, mockResult);
Expand All @@ -209,6 +212,7 @@ public void chooseMultiImageFromGallery_WhenPendingResultExists_FinishesWithAlre
chooseVideoFromGallery_WhenHasExternalStoragePermission_LaunchesChooseFromGalleryIntent() {
when(mockPermissionManager.isPermissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE))
.thenReturn(true);
when(mockMethodCall.argument("useAndroidPhotoPicker")).thenReturn(true);

ImagePickerDelegate delegate = createDelegate();
delegate.chooseVideoFromGallery(mockMethodCall, mockResult);
Expand All @@ -224,6 +228,7 @@ public void chooseMultiImageFromGallery_WhenPendingResultExists_FinishesWithAlre
chooseVideoFromGallery_WithPhotoPicker_WhenHasExternalStoragePermission_LaunchesChooseFromGalleryIntent() {
when(mockPermissionManager.isPermissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE))
.thenReturn(true);
when(mockMethodCall.argument("useAndroidPhotoPicker")).thenReturn(true);

ImagePickerDelegate delegate = createDelegate();
delegate.chooseVideoFromGallery(mockMethodCall, mockResult);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
targets:
$default:
sources:
include:
- lib/**
# Some default includes that aren't really used here but will prevent
# false-negative warnings:
- $package$
- lib/$lib$
exclude:
- '**/.*/**'
- '**/build/**'
builders:
code_excerpter|code_excerpter:
enabled: true
11 changes: 11 additions & 0 deletions packages/image_picker/image_picker_android/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_driver/driver_extension.dart';
// #docregion photo-picker-example
import 'package:image_picker_android/image_picker_android.dart';
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
// #enddocregion photo-picker-example
import 'package:video_player/video_player.dart';

void appMain() {
Expand All @@ -19,6 +22,14 @@ void appMain() {
}

void main() {
// Set to use Android Photo Picker.
// #docregion photo-picker-example
final ImagePickerPlatform imagePickerImplementation =
ImagePickerPlatform.instance;
if (imagePickerImplementation is ImagePickerAndroid) {
imagePickerImplementation.useAndroidPhotoPicker = true;
}
// #enddocregion photo-picker-example
runApp(const MyApp());
}

Expand Down
1 change: 1 addition & 0 deletions packages/image_picker/image_picker_android/example/pubspec.yaml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
video_player: ^2.1.4

dev_dependencies:
build_runner: ^2.1.10
espresso: ^0.2.0
flutter_test:
sdk: flutter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class ImagePickerAndroid extends ImagePickerPlatform {
'maxWidth': maxWidth,
'maxHeight': maxHeight,
'imageQuality': imageQuality,
'useAndroidPhotoPicker': useAndroidPhotoPicker,
},
);
}
Expand Down Expand Up @@ -117,6 +118,7 @@ class ImagePickerAndroid extends ImagePickerPlatform {
'imageQuality': imageQuality,
'cameraDevice': preferredCameraDevice.index,
'requestFullMetadata': requestFullMetadata,
'useAndroidPhotoPicker': useAndroidPhotoPicker,
},
);
}
Expand Down Expand Up @@ -145,7 +147,8 @@ class ImagePickerAndroid extends ImagePickerPlatform {
<String, dynamic>{
'source': source.index,
'maxDuration': maxDuration?.inSeconds,
'cameraDevice': preferredCameraDevice.index
'cameraDevice': preferredCameraDevice.index,
'useAndroidPhotoPicker': useAndroidPhotoPicker,
},
);
}
Expand Down Expand Up @@ -279,4 +282,9 @@ class ImagePickerAndroid extends ImagePickerPlatform {
files: pickedFileList,
);
}

/// Set [ImagePickerAndroid] to use Android 13 Photo Picker.
///
/// Currently defaults to false, but the default is subject to change.
bool useAndroidPhotoPicker = false;
}
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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.5+10
version: 0.8.6

environment:
sdk: ">=2.17.0 <3.0.0"
Expand Down
Loading

0 comments on commit 3814ec4

Please sign in to comment.