Skip to content

Conversation

@mirulumam
Copy link

tested on Android 7.1

mirulumam and others added 3 commits January 18, 2017 16:37
Getting Storage permission for Android 6+
@mirulumam mirulumam closed this Feb 25, 2017
@mirulumam mirulumam reopened this Feb 25, 2017
@nanmu42
Copy link

nanmu42 commented Jun 6, 2017

Hey, buddy, you should really consider accepting this. It works like charm.

@NivaLevy
Copy link

NivaLevy commented Jan 7, 2019

Thank you for this.
I think it would be better not to add another class and just take care of it all in the ImagePicker.java file like so:

/**

  • An Image Picker Plugin for Cordova/PhoneGap.
    */
    package com.synconset;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.cordova.PermissionHelper;

import java.util.ArrayList;
import android.Manifest;

import android.content.pm.PackageManager;
import android.os.Build;
import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.util.Log;

public class ImagePicker extends CordovaPlugin {
public static String TAG = "ImagePicker";

private CallbackContext callbackContext;
private JSONObject params;
public static final int SAVE_TO_ALBUM_SEC = 1;


public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
	 this.callbackContext = callbackContext;
	 this.params = args.getJSONObject(0);

	 Context context = this.cordova.getActivity().getApplicationContext();

	 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if(!PermissionHelper.hasPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
			PermissionHelper.requestPermission(this, SAVE_TO_ALBUM_SEC, Manifest.permission.READ_EXTERNAL_STORAGE);
        } else {
			startGettingPics(action) ;
		}
    } else {
		startGettingPics(action) ;
	}
	return true;
}


public void startGettingPics(String action) throws JSONException{
	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);
		}
	}
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
	if (resultCode == Activity.RESULT_OK && data != null) {
		ArrayList<String> fileNames = data.getStringArrayListExtra("MULTIPLEFILENAMES");
		JSONArray res = new JSONArray(fileNames);
		this.callbackContext.success(res);
	} else if (resultCode == Activity.RESULT_CANCELED && data != null) {
		String error = data.getStringExtra("ERRORMESSAGE");
		this.callbackContext.error(error);
	} else if (resultCode == Activity.RESULT_CANCELED) {
		JSONArray res = new JSONArray();
		this.callbackContext.success(res);
	} else {
		this.callbackContext.error("No images selected");
	}
}

public void onRequestPermissionResult(int requestCode, String[] permissions,
                                      int[] grantResults) throws JSONException {
    for (int r : grantResults) {
        if (r == PackageManager.PERMISSION_DENIED) {
            return;
        }
    }
    switch (requestCode) {
     
        case SAVE_TO_ALBUM_SEC:
            this.startGettingPics("getPictures");
            break;
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants