Skip to content

Commit

Permalink
feat(android): permit to specify a list of allowed mime-types
Browse files Browse the repository at this point in the history
  • Loading branch information
renchap committed Jun 16, 2023
1 parent 8e4b205 commit ac28ad4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 34 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,23 @@ The `callback` will be called with a response object, refer to [The Response Obj
## Options
| Option | iOS | Android | Web | Description |
| ----------------- | --- | ------- | --- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| mediaType | OK | OK | OK | 'photo' or 'video' or 'mixed'(launchCamera on Android does not support 'mixed'). Web only supports 'photo' for now. |
| maxWidth | OK | OK | NO | To resize the image |
| maxHeight | OK | OK | NO | To resize the image |
| videoQuality | OK | OK | NO | 'low', 'medium', or 'high' on iOS, 'low' or 'high' on Android |
| durationLimit | OK | OK | NO | Video max duration in seconds |
| quality | OK | OK | NO | 0 to 1, photos |
| cameraType | OK | OK | NO | 'back' or 'front'. May not be supported in few android devices |
| includeBase64 | OK | OK | OK | If true, creates base64 string of the image (Avoid using on large image files due to performance) |
| includeExtra | OK | OK | NO | If true, will include extra data which requires library permissions to be requested (i.e. exif data) |
| saveToPhotos | OK | OK | NO | (Boolean) Only for launchCamera, saves the image/video file captured to public photo |
| selectionLimit | OK | OK | OK | Default is `1`, use `0` to allow any number of files. Only iOS version >= 14 & Android version >= 13 support `0` and also it supports providing any integer value |
| presentationStyle | OK | NO | NO | Controls how the picker is presented. 'pageSheet', 'fullScreen', 'pageSheet', 'formSheet', 'popover', 'overFullScreen', 'overCurrentContext'. Default is 'currentContext' |
| formatAsMp4 | OK | NO | NO | Converts the selected video to MP4. iOS Only. |
| assetRepresentationMode | OK | NO | NO | A mode that determines which representation to use if an asset contains more than one. Possible values: 'auto', 'current', 'compatible'. Default is 'auto' |
|
| Option | iOS | Android | Web | Description |
| ----------------------- | --- | ------- | --- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| mediaType | OK | OK | OK | 'photo' or 'video' or 'mixed'(launchCamera on Android does not support 'mixed'). Web only supports 'photo' for now. |
| mediaType | KO | OK | KO | Array containing the mime-types allowed to be picked. Default is empty (everything). |
| maxWidth | OK | OK | NO | To resize the image |
| maxHeight | OK | OK | NO | To resize the image |
| videoQuality | OK | OK | NO | 'low', 'medium', or 'high' on iOS, 'low' or 'high' on Android |
| durationLimit | OK | OK | NO | Video max duration in seconds |
| quality | OK | OK | NO | 0 to 1, photos |
| cameraType | OK | OK | NO | 'back' or 'front'. May not be supported in few android devices |
| includeBase64 | OK | OK | OK | If true, creates base64 string of the image (Avoid using on large image files due to performance) |
| includeExtra | OK | OK | NO | If true, will include extra data which requires library permissions to be requested (i.e. exif data) |
| saveToPhotos | OK | OK | NO | (Boolean) Only for launchCamera, saves the image/video file captured to public photo |
| selectionLimit | OK | OK | OK | Default is `1`, use `0` to allow any number of files. Only iOS version >= 14 & Android version >= 13 support `0` and also it supports providing any integer value |
| presentationStyle | OK | NO | NO | Controls how the picker is presented. 'pageSheet', 'fullScreen', 'pageSheet', 'formSheet', 'popover', 'overFullScreen', 'overCurrentContext'. Default is 'currentContext' |
| formatAsMp4 | OK | NO | NO | Converts the selected video to MP4. iOS Only. |
| assetRepresentationMode | OK | NO | NO | A mode that determines which representation to use if an asset contains more than one. Possible values: 'auto', 'current', 'compatible'. Default is 'auto' |
## The Response Object
Expand Down
21 changes: 14 additions & 7 deletions android/src/main/java/com/imagepicker/ImagePickerModuleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,35 +125,42 @@ public void launchImageLibrary(final ReadableMap options, final Callback callbac
boolean isPhoto = this.options.mediaType.equals(mediaTypePhoto);
boolean isVideo = this.options.mediaType.equals(mediaTypeVideo);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
boolean usePhotoPicker = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU;

if (usePhotoPicker) {
libraryIntent = new Intent(MediaStore.ACTION_PICK_IMAGES);
} else {
if (isSingleSelect && (isPhoto || isVideo)) {
libraryIntent = new Intent(Intent.ACTION_PICK);
} else {
libraryIntent = new Intent(Intent.ACTION_GET_CONTENT);
libraryIntent.addCategory(Intent.CATEGORY_OPENABLE);
}
} else {
libraryIntent = new Intent(MediaStore.ACTION_PICK_IMAGES);
}

if (!isSingleSelect) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
libraryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
} else {
if (usePhotoPicker) {
if (selectionLimit != 1) {
int maxNum = selectionLimit;
if (selectionLimit == 0) maxNum = MediaStore.getPickImagesMaxLimit();
libraryIntent.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, maxNum);
}
} else {
libraryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
}

if (isPhoto) {
libraryIntent.setType("image/*");
} else if (isVideo) {
libraryIntent.setType("video/*");
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
} else if (!usePhotoPicker) {
libraryIntent.setType("*/*");
}

if(this.options.restrictMimeTypes.length > 0) {
libraryIntent.putExtra(Intent.EXTRA_MIME_TYPES, this.options.restrictMimeTypes);
} else if(!usePhotoPicker) {
libraryIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"});
}

Expand Down
6 changes: 5 additions & 1 deletion android/src/main/java/com/imagepicker/Options.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.imagepicker;

import java.util.stream.Collectors;
import com.facebook.react.bridge.ReadableMap;
import android.text.TextUtils;

Expand All @@ -15,10 +16,13 @@ public class Options {
int durationLimit;
Boolean useFrontCamera = false;
String mediaType;

String[] restrictMimeTypes;

Options(ReadableMap options) {
mediaType = options.getString("mediaType");
restrictMimeTypes = options.getArray("restrictMimeTypes").toArrayList().stream()
.map(Object::toString)
.toArray(size -> new String[size]);
selectionLimit = options.getInt("selectionLimit");
includeBase64 = options.getBoolean("includeBase64");
includeExtra = options.getBoolean("includeExtra");
Expand Down
2 changes: 1 addition & 1 deletion example/src/components/DemoResponse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function DemoResponse({children}: React.PropsWithChildren<{}>) {
}

return (
<ScrollView style={styles.container}>
<ScrollView style={styles.container} nestedScrollEnabled>
<Text style={styles.text}>{JSON.stringify(children, null, 2)}</Text>
</ScrollView>
);
Expand Down
7 changes: 4 additions & 3 deletions src/platforms/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {

const DEFAULT_OPTIONS: ImageLibraryOptions & CameraOptions = {
mediaType: 'photo',
restrictMimeTypes: [],
videoQuality: 'high',
quality: 1,
maxWidth: 0,
Expand All @@ -26,9 +27,9 @@ const DEFAULT_OPTIONS: ImageLibraryOptions & CameraOptions = {
// @ts-ignore We want to check whether __turboModuleProxy exitst, it may not
const isTurboModuleEnabled = global.__turboModuleProxy != null;

const nativeImagePicler = isTurboModuleEnabled ?
require("./NativeImagePicker").default :
NativeModules.ImagePicker;
const nativeImagePicler = isTurboModuleEnabled
? require('./NativeImagePicker').default
: NativeModules.ImagePicker;

export function camera(
options: CameraOptions,
Expand Down
8 changes: 3 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ export interface OptionsCommon {
| 'formSheet'
| 'popover'
| 'overFullScreen'
| 'overCurrentContext'
assetRepresentationMode?:
| 'auto'
| 'current'
| 'compatible';
| 'overCurrentContext';
assetRepresentationMode?: 'auto' | 'current' | 'compatible';
}

export interface ImageLibraryOptions extends OptionsCommon {
selectionLimit?: number;
restrictMimeTypes?: string[];
}

export interface CameraOptions extends OptionsCommon {
Expand Down

0 comments on commit ac28ad4

Please sign in to comment.