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 May 7, 2024
1 parent 799fa49 commit 55149ba
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 52 deletions.
72 changes: 37 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ npx pod-install ios

Add the appropriate keys to your `Info.plist` depending on your requirement:

| Requirement | Key |
| ------------------------------ | --------------------------------------------------- |
| Select image/video from photos | NSPhotoLibraryUsageDescription |
| Capture Image | NSCameraUsageDescription |
| Requirement | Key |
| ------------------------------ | ------------------------------------------------------- |
| Select image/video from photos | NSPhotoLibraryUsageDescription |
| Capture Image | NSCameraUsageDescription |
| Capture Video | NSCameraUsageDescription & NSMicrophoneUsageDescription |

### Android
Expand Down Expand Up @@ -90,23 +90,25 @@ 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 | Supports providing any integer value. Use `0` to allow any number of files on iOS version >= 14 & Android version >= 13. Default is `1`. |
| presentationStyle | OK | NO | NO | Controls how the picker is presented. `currentContext`, `pageSheet`, `fullScreen`, `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. |
| restrictMimeTypes | 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 All @@ -119,20 +121,20 @@ The `callback` will be called with a response object, refer to [The Response Obj
## Asset Object
| key | iOS | Android | Web | Photo/Video | Requires Permissions | Description |
| --------- | --- | ------- | --- | ----------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| base64 | OK | OK | OK | PHOTO ONLY | NO | The base64 string of the image (photos only) |
| uri | OK | OK | OK | BOTH | NO | The file uri in app specific cache storage. Except when picking **video from Android gallery** where you will get read only content uri, to get file uri in this case copy the file to app specific storage using any react-native library. For web it uses the base64 as uri. |
| originalPath | NO | OK | NO | BOTH | NO | The original file path. |
| width | OK | OK | OK | BOTH | NO | Asset dimensions |
| height | OK | OK | OK | BOTH | NO | Asset dimensions |
| fileSize | OK | OK | NO | BOTH | NO | The file size |
| type | OK | OK | NO | BOTH | NO | The file type |
| fileName | OK | OK | NO | BOTH | NO | The file name |
| duration | OK | OK | NO | VIDEO ONLY | NO | The selected video duration in seconds |
| bitrate | --- | OK | NO | VIDEO ONLY | NO | The average bitrate (in bits/sec) of the selected video, if available. (Android only) |
| timestamp | OK | OK | NO | BOTH | YES | Timestamp of the asset. Only included if 'includeExtra' is true |
| id | OK | OK | NO | BOTH | YES | local identifier of the photo or video. On Android, this is the same as fileName |
| key | iOS | Android | Web | Photo/Video | Requires Permissions | Description |
| ------------ | --- | ------- | --- | ----------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| base64 | OK | OK | OK | PHOTO ONLY | NO | The base64 string of the image (photos only) |
| uri | OK | OK | OK | BOTH | NO | The file uri in app specific cache storage. Except when picking **video from Android gallery** where you will get read only content uri, to get file uri in this case copy the file to app specific storage using any react-native library. For web it uses the base64 as uri. |
| originalPath | NO | OK | NO | BOTH | NO | The original file path. |
| width | OK | OK | OK | BOTH | NO | Asset dimensions |
| height | OK | OK | OK | BOTH | NO | Asset dimensions |
| fileSize | OK | OK | NO | BOTH | NO | The file size |
| type | OK | OK | NO | BOTH | NO | The file type |
| fileName | OK | OK | NO | BOTH | NO | The file name |
| duration | OK | OK | NO | VIDEO ONLY | NO | The selected video duration in seconds |
| bitrate | --- | OK | NO | VIDEO ONLY | NO | The average bitrate (in bits/sec) of the selected video, if available. (Android only) |
| timestamp | OK | OK | NO | BOTH | YES | Timestamp of the asset. Only included if 'includeExtra' is true |
| id | OK | OK | NO | BOTH | YES | local identifier of the photo or video. On Android, this is the same as fileName |
## Note on file storage
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

0 comments on commit 55149ba

Please sign in to comment.