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 1 commit
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
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