Skip to content

Commit

Permalink
use pickvisualmedia api
Browse files Browse the repository at this point in the history
  • Loading branch information
HashirRajah committed Feb 13, 2024
1 parent 98d56d9 commit 714b168
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
</config-file>

<framework src="com.android.support:appcompat-v7:27.1.1" />
<framework src="androidx.activity:activity:1.7.+" />

<source-file src="src/android/ImagePicker.java" target-dir="src/com/spoon/imagepicker" />
</platform>
Expand Down
21 changes: 17 additions & 4 deletions src/android/ImagePicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;

public class ImagePicker extends CordovaPlugin {
private static final String ACTION_GET_PICTURES = "getPictures";
private static final String ACTION_HAS_READ_PERMISSION = "hasReadPermission";
Expand All @@ -65,10 +69,19 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
final JSONObject params = args.getJSONObject(0);
this.maxImageCount = params.has("maximumImagesCount") ? params.getInt("maximumImagesCount") : 20;

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);
ActivityResultLauncher<PickVisualMediaRequest> pickMultipleMedia =
cordova.getActivity().registerForActivityResult(new ActivityResultContracts.PickMultipleVisualMedia(5), uris -> {
// Callback is invoked after the user selects media items or closes the
// photo picker.
if (!uris.isEmpty()) {
Log.d("PhotoPicker", "Number of items selected: " + uris.size());
} else {
Log.d("PhotoPicker", "No media selected");
}
});
pickMultipleMedia.launch(new PickVisualMediaRequest.Builder()
.setMediaType(ActivityResultContracts.PickVisualMedia.ImageAndVideo.INSTANCE)
.build());
this.showMaxLimitWarning();
return true;
}
Expand Down

0 comments on commit 714b168

Please sign in to comment.