From 6fbe7d028ee5b9ccbfbf4739e3cc4ddb1ef22256 Mon Sep 17 00:00:00 2001 From: wux-shuto <39753363+wux-shuto@users.noreply.github.com> Date: Mon, 6 May 2019 17:32:51 +0800 Subject: [PATCH] request permission --- .../synconset/ImagePicker/ImagePicker.java | 69 +++++++++++-------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/src/android/com/synconset/ImagePicker/ImagePicker.java b/src/android/com/synconset/ImagePicker/ImagePicker.java index b1ad18d..36a67fb 100644 --- a/src/android/com/synconset/ImagePicker/ImagePicker.java +++ b/src/android/com/synconset/ImagePicker/ImagePicker.java @@ -6,12 +6,14 @@ package com.synconset; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PermissionHelper; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; +import android.Manifest; import android.app.Activity; import android.content.Intent; import android.util.Log; @@ -23,37 +25,50 @@ public class ImagePicker extends CordovaPlugin { private JSONObject params; public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException { - this.callbackContext = callbackContext; - this.params = args.getJSONObject(0); - if (action.equals("getPictures")) { - Intent intent = new Intent(cordova.getActivity(), MultiImageChooserActivity.class); - int max = 20; - int desiredWidth = 0; - int desiredHeight = 0; - int quality = 100; - if (this.params.has("maximumImagesCount")) { - max = this.params.getInt("maximumImagesCount"); - } - if (this.params.has("width")) { - desiredWidth = this.params.getInt("width"); - } - if (this.params.has("height")) { - desiredHeight = this.params.getInt("height"); - } - if (this.params.has("quality")) { - quality = this.params.getInt("quality"); - } - intent.putExtra("MAX_IMAGES", max); - intent.putExtra("WIDTH", desiredWidth); - intent.putExtra("HEIGHT", desiredHeight); - intent.putExtra("QUALITY", quality); - if (this.cordova != null) { - this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0); + this.callbackContext = callbackContext; + this.params = args.getJSONObject(0); + if(!PermissionHelper.hasPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) { + PermissionHelper.requestPermission(this, 1, Manifest.permission.READ_EXTERNAL_STORAGE); + } else { + if (action.equals("getPictures")) { + getPictures(); } } return true; } - + + private void getPictures() throws JSONException { + Intent intent = new Intent(cordova.getActivity(), MultiImageChooserActivity.class); + int max = 20; + int desiredWidth = 0; + int desiredHeight = 0; + int quality = 100; + if (this.params.has("maximumImagesCount")) { + max = this.params.getInt("maximumImagesCount"); + } + if (this.params.has("width")) { + desiredWidth = this.params.getInt("width"); + } + if (this.params.has("height")) { + desiredHeight = this.params.getInt("height"); + } + if (this.params.has("quality")) { + quality = this.params.getInt("quality"); + } + intent.putExtra("MAX_IMAGES", max); + intent.putExtra("WIDTH", desiredWidth); + intent.putExtra("HEIGHT", desiredHeight); + intent.putExtra("QUALITY", quality); + if (this.cordova != null) { + this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0); + } + } + + public void onRequestPermissionResult(int requestCode, String[] permissions, + int[] grantResults) throws JSONException { + getPictures(); + } + public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK && data != null) { ArrayList fileNames = data.getStringArrayListExtra("MULTIPLEFILENAMES");