|
13 | 13 | import android.net.Uri;
|
14 | 14 | import android.os.Build;
|
15 | 15 | import android.os.Bundle;
|
| 16 | +import android.os.ext.SdkExtensions; |
16 | 17 | import android.provider.MediaStore;
|
17 | 18 | import android.provider.OpenableColumns;
|
18 | 19 | import android.util.Log;
|
|
23 | 24 | import android.widget.ProgressBar;
|
24 | 25 | import android.widget.Toast;
|
25 | 26 |
|
| 27 | +import androidx.activity.result.PickVisualMediaRequest; |
| 28 | +import androidx.activity.result.contract.ActivityResultContracts; |
26 | 29 | import androidx.core.content.ContextCompat;
|
27 | 30 |
|
28 | 31 | import org.apache.cordova.CallbackContext;
|
@@ -64,12 +67,28 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
|
64 | 67 | } else if (ACTION_GET_PICTURES.equals(action)) {
|
65 | 68 | final JSONObject params = args.getJSONObject(0);
|
66 | 69 | this.maxImageCount = params.has("maximumImagesCount") ? params.getInt("maximumImagesCount") : 20;
|
| 70 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && SdkExtensions.getExtensionVersion(Build.VERSION_CODES.R) >= 2) { |
| 71 | + int deviceMaxLimit = MediaStore.getPickImagesMaxLimit(); |
| 72 | + if (this.maxImageCount > deviceMaxLimit) { |
| 73 | + this.maxImageCount = deviceMaxLimit; |
| 74 | + this.showMaxLimitWarning(deviceMaxLimit); |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + boolean useFilePicker = params.has("useFilePicker") && params.getBoolean("useFilePicker"); |
| 79 | + |
| 80 | + Intent imagePickerIntent = null; |
| 81 | + if (useFilePicker) { |
| 82 | + imagePickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
| 83 | + imagePickerIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); |
| 84 | + imagePickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); |
| 85 | + } else { |
| 86 | + PickVisualMediaRequest pickVisualMediaRequest = new PickVisualMediaRequest.Builder().setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE).build(); |
| 87 | + imagePickerIntent = new ActivityResultContracts.PickMultipleVisualMedia(maxImageCount).createIntent(cordova.getContext(), pickVisualMediaRequest); |
| 88 | + } |
67 | 89 |
|
68 |
| - Intent imagePickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); |
69 |
| - imagePickerIntent.setType("image/*"); |
70 |
| - imagePickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); |
71 | 90 | cordova.startActivityForResult(this, imagePickerIntent, SELECT_PICTURE);
|
72 |
| - this.showMaxLimitWarning(); |
| 91 | + this.showMaxLimitWarning(useFilePicker); |
73 | 92 | return true;
|
74 | 93 | }
|
75 | 94 | return false;
|
@@ -266,8 +285,18 @@ private String copyFileToInternalStorage(Uri uri, String newDirName) {
|
266 | 285 | return output.getPath();
|
267 | 286 | }
|
268 | 287 |
|
269 |
| - private void showMaxLimitWarning() { |
270 |
| - String toastMsg = "Only the first " + this.maxImageCount + " images selected will be taken."; |
| 288 | + private void showMaxLimitWarning(boolean useFilePicker) { |
| 289 | + String toastMsg = "You can only select up to " + this.maxImageCount + " image(s)"; |
| 290 | + if (useFilePicker) { |
| 291 | + toastMsg = "Only the first " + this.maxImageCount + " image(s) selected will be taken."; |
| 292 | + } |
| 293 | + (Toast.makeText(cordova.getContext(), toastMsg, Toast.LENGTH_LONG)).show(); |
| 294 | + } |
| 295 | + private void showMaxLimitWarning(int deviceMaxLimit) { |
| 296 | + String toastMsg = "The maximumImagesCount:" + this.maxImageCount + |
| 297 | + "is greater than the device's max limit of images that can be selected from the MediaStore:" + deviceMaxLimit + |
| 298 | + ". Maximum number of images that can be selected is:" + deviceMaxLimit; |
| 299 | + |
271 | 300 | (Toast.makeText(cordova.getContext(), toastMsg, Toast.LENGTH_LONG)).show();
|
272 | 301 | }
|
273 | 302 | }
|
0 commit comments