Skip to content

add picke visual media api #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [2.2.0](https://github.com/spoonconsulting/cordova-plugin-telerik-imagepicker/compare/2.1.1...2.2.0) (2025-02-28)

* **Android:** Now using the recommended image picker by Android (PickVisualMedia) instead of file picker. However an option to revert to file picker has been added: `useFilePicker: true`

## [2.1.2](https://github.com/spoonconsulting/cordova-plugin-telerik-imagepicker/compare/2.1.1...2.1.2) (2024-05-27)

* **ios:** Bug Fix.
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ window.imagePicker.getPictures(
// available options are
// window.imagePicker.OutputType.FILE_URI (0) or
// window.imagePicker.OutputType.BASE64_STRING (1)
outputType: int
outputType: int,

// option to revert to file picker on android
useFilePicker: boolean

};

### Note for Android Use

The 'maximumImagesCount' option is currently the only option supported for Android

The `useFilePicker` option is only used for Android as prior to the current version the file picker was being used.

## Android 6 (M) Permissions
On Android 6 you need to request permission to read external storage at runtime when targeting API level 23+.
Even if the `uses-permission` tags for the Calendar are present in `AndroidManifest.xml`.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spoonconsulting/cordova-plugin-telerik-imagepicker",
"version": "2.1.2",
"version": "2.2.0",
"cordova_name": "ImagePicker",
"description": "This plugin allows selection of multiple images from the camera roll / gallery in a phonegap app",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="@spoonconsulting/cordova-plugin-telerik-imagepicker"
version="2.1.2">
version="2.2.0">

<name>ImagePicker</name>

Expand Down
41 changes: 35 additions & 6 deletions src/android/ImagePicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.ext.SdkExtensions;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.util.Log;
Expand All @@ -23,6 +24,8 @@
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.core.content.ContextCompat;

import org.apache.cordova.CallbackContext;
Expand Down Expand Up @@ -64,12 +67,28 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
} else if (ACTION_GET_PICTURES.equals(action)) {
final JSONObject params = args.getJSONObject(0);
this.maxImageCount = params.has("maximumImagesCount") ? params.getInt("maximumImagesCount") : 20;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && SdkExtensions.getExtensionVersion(Build.VERSION_CODES.R) >= 2) {
int deviceMaxLimit = MediaStore.getPickImagesMaxLimit();
if (this.maxImageCount > deviceMaxLimit) {
this.maxImageCount = deviceMaxLimit;
this.showMaxLimitWarning(deviceMaxLimit);
};
}

boolean useFilePicker = params.has("useFilePicker") && params.getBoolean("useFilePicker");

Intent imagePickerIntent = null;
if (useFilePicker) {
imagePickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
imagePickerIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
imagePickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
} else {
PickVisualMediaRequest pickVisualMediaRequest = new PickVisualMediaRequest.Builder().setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE).build();
imagePickerIntent = new ActivityResultContracts.PickMultipleVisualMedia(maxImageCount).createIntent(cordova.getContext(), pickVisualMediaRequest);
}

Intent imagePickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imagePickerIntent.setType("image/*");
imagePickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
cordova.startActivityForResult(this, imagePickerIntent, SELECT_PICTURE);
this.showMaxLimitWarning();
this.showMaxLimitWarning(useFilePicker);
return true;
}
return false;
Expand Down Expand Up @@ -266,8 +285,18 @@ private String copyFileToInternalStorage(Uri uri, String newDirName) {
return output.getPath();
}

private void showMaxLimitWarning() {
String toastMsg = "Only the first " + this.maxImageCount + " images selected will be taken.";
private void showMaxLimitWarning(boolean useFilePicker) {
String toastMsg = "You can only select up to " + this.maxImageCount + " image(s)";
if (useFilePicker) {
toastMsg = "Only the first " + this.maxImageCount + " image(s) selected will be taken.";
}
(Toast.makeText(cordova.getContext(), toastMsg, Toast.LENGTH_LONG)).show();
}
private void showMaxLimitWarning(int deviceMaxLimit) {
String toastMsg = "The maximumImagesCount:" + this.maxImageCount +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String toastMsg = "The maximumImagesCount:" + this.maxImageCount +
String toastMsg = "The maximumImagesCount: " + this.maxImageCount +

" is greater than the device's max limit of images that can be selected from the MediaStore: " + deviceMaxLimit +
". Maximum number of images that can be selected is: " + deviceMaxLimit;

(Toast.makeText(cordova.getContext(), toastMsg, Toast.LENGTH_LONG)).show();
}
}
1 change: 1 addition & 0 deletions www/imagepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ ImagePicker.prototype.getPictures = function(success, fail, options) {

var params = {
maximumImagesCount: options.maximumImagesCount ? options.maximumImagesCount : 15,
useFilePicker: options.useFilePicker ? options.useFilePicker : false,
width: options.width ? options.width : 0,
height: options.height ? options.height : 0,
quality: options.quality ? options.quality : 100,
Expand Down