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

import org.apache.cordova.PermissionHelper;

import android.Manifest;
import android.content.pm.PermissionInfo;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

public class ImagePicker extends CordovaPlugin {
    public static String TAG = "ImagePicker";
    protected final static String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE};
    private CallbackContext callbackContext;
    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) {
                try {
                    if (!PermissionHelper.hasPermission(this, permissions[0])) {
                        PermissionHelper.requestPermission(this, 1, Manifest.permission.READ_EXTERNAL_STORAGE);
                    } else {
                        this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0);
                    }
                } catch (IllegalArgumentException e) {
                    callbackContext.error("Illegal Argument Exception");
                    return false;
                }
            }
        }
        return true;
    }

    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");
        }
    }
}

@@ -1028,24 +1028,261 @@
 ctures%22)) %7B%0A
+%0A            try %7B%0A                if (!PermissionHelper.hasPermission(this, permissions%5B0%5D)) %7B%0A                    PermissionHelper.requestPermission(this, 1, Manifest.permission.READ_EXTERNAL_STORAGE);%0A                %7D else %7B%0A        
             
@@ -1373,24 +1373,32 @@
             
+        
 int max = 20
@@ -1403,32 +1403,40 @@
 20;%0A            
+   
+     
 int desiredWidth
@@ -1445,32 +1445,40 @@
  0;%0A            
+      
+  
 int desiredHeigh
@@ -1492,24 +1492,32 @@
             
+        
 int quality 
@@ -1527,32 +1527,40 @@
 00;%0A            
+ 
+       
 if (this.params.
@@ -1604,16 +1604,24 @@
         
+    
+    
 max = th
@@ -1664,34 +1664,50 @@
 %22);%0A            
+       
+ 
 %7D%0A
+        
             if (
@@ -1746,24 +1746,32 @@
             
+ 
+       
 desiredWidth
@@ -1806,34 +1806,49 @@
 %22);%0A            
-%7D%0A
+        %7D%0A       
             if (
@@ -1835,32 +1835,33 @@
                 
+ 
 if (this.params.
@@ -1873,24 +1873,32 @@
 height%22)) %7B%0A
+        
             
@@ -1951,34 +1951,46 @@
 %22);%0A            
-%7D%0A
+        %7D%0A    
             if (
@@ -1977,32 +1977,36 @@
                 
+    
 if (this.params.
@@ -2019,24 +2019,32 @@
 uality%22)) %7B%0A
+        
             
@@ -2092,34 +2092,44 @@
 %22);%0A            
-%7D%0A
+        %7D%0A  
             inte
@@ -2116,32 +2116,38 @@
 %7D%0A              
+      
 intent.putExtra(
@@ -2170,32 +2170,40 @@
 x);%0A            
+     
+   
 intent.putExtra(
@@ -2230,32 +2230,40 @@
 h);%0A            
+       
+ 
 intent.putExtra(
@@ -2292,32 +2292,40 @@
 t);%0A            
+        
 intent.putExtra(
@@ -2349,32 +2349,40 @@
 y);%0A            
+  
+      
 if (this.cordova
@@ -2397,252 +2397,8 @@
 ) %7B%0A
-                try %7B%0A                    if (!PermissionHelper.hasPermission(this, permissions%5B0%5D)) %7B%0A                        PermissionHelper.requestPermission(this, 1, Manifest.permission.READ_EXTERNAL_STORAGE);%0A                    %7D else %7B%0A
     
@@ -2526,16 +2526,30 @@
        %7D
+%0A            %7D
  catch (
@@ -2574,28 +2574,24 @@
 eption e) %7B%0A
-    
             
@@ -2659,28 +2659,24 @@
             
- 
-   
 return false
@@ -2677,34 +2677,16 @@
  false;%0A
-                %7D%0A
         

Update ImagePicker.java