Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* An Image Picker Plugin for Cordova/PhoneGap.
*/
package com.synconset;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
public class ImagePicker extends CordovaPlugin {
private static final String ACTION_GET_PICTURES = "getPictures";
private static final String ACTION_HAS_READ_PERMISSION = "hasReadPermission";
private static final String ACTION_REQUEST_READ_PERMISSION = "requestReadPermission";
private static final int PERMISSION_REQUEST_CODE = 100;
private CallbackContext callbackContext;
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
if (ACTION_HAS_READ_PERMISSION.equals(action)) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, hasReadPermission()));
return true;
} else if (ACTION_REQUEST_READ_PERMISSION.equals(action)) {
requestReadPermission();
return true;
} else if (ACTION_GET_PICTURES.equals(action)) {
final JSONObject params = args.getJSONObject(0);
final Intent imagePickerIntent = new Intent(cordova.getActivity(), MultiImageChooserActivity.class);
int max = 20;
int desiredWidth = 0;
int desiredHeight = 0;
int quality = 100;
int outputType = 0;
if (params.has("maximumImagesCount")) {
max = params.getInt("maximumImagesCount");
}
if (params.has("width")) {
desiredWidth = params.getInt("width");
}
if (params.has("height")) {
desiredHeight = params.getInt("height");
}
if (params.has("quality")) {
quality = params.getInt("quality");
}
if (params.has("outputType")) {
outputType = params.getInt("outputType");
}
imagePickerIntent.putExtra("MAX_IMAGES", max);
imagePickerIntent.putExtra("WIDTH", desiredWidth);
imagePickerIntent.putExtra("HEIGHT", desiredHeight);
imagePickerIntent.putExtra("QUALITY", quality);
imagePickerIntent.putExtra("OUTPUT_TYPE", outputType);
// some day, when everybody uses a cordova version supporting 'hasPermission', enable this:
/*
if (cordova != null) {
if (cordova.hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
cordova.startActivityForResult(this, imagePickerIntent, 0);
} else {
cordova.requestPermission(
this,
PERMISSION_REQUEST_CODE,
Manifest.permission.READ_EXTERNAL_STORAGE
);
}
}
*/
// .. until then use:
if (hasReadPermission()) {
cordova.startActivityForResult(this, imagePickerIntent, 0);
} else {
requestReadPermission();
// The downside is the user needs to re-invoke this picker method.
// The best thing to do for the dev is check 'hasReadPermission' manually and
// run 'requestReadPermission' or 'getPictures' based on the outcome.
}
return true;
}
return false;
}
@SuppressLint("InlinedApi")
private boolean hasReadPermission() {
return Build.VERSION.SDK_INT < 23 ||
PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this.cordova.getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
}
@SuppressLint("InlinedApi")
private void requestReadPermission() {
if (!hasReadPermission()) {
ActivityCompat.requestPermissions(
this.cordova.getActivity(),
new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
// This method executes async and we seem to have no known way to receive the result
// (that's why these methods were later added to Cordova), so simply returning ok now.
callbackContext.success();
}
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);
callbackContext.success(res);
} else if (resultCode == Activity.RESULT_CANCELED && data != null) {
String error = data.getStringExtra("ERRORMESSAGE");
callbackContext.error(error);
} else if (resultCode == Activity.RESULT_CANCELED) {
JSONArray res = new JSONArray();
callbackContext.success(res);
} else {
callbackContext.error("No images selected");
}
}
/**
* Choosing a picture launches another Activity, so we need to implement the
* save/restore APIs to handle the case where the CordovaActivity is killed by the OS
* before we get the launched Activity's result.
*
* @see http://cordova.apache.org/docs/en/dev/guide/platforms/android/plugin.html#launching-other-activities
*/
public void onRestoreStateForActivityResult(Bundle state, CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}
/*
@Override
public void onRequestPermissionResult(int requestCode,
String[] permissions,
int[] grantResults) throws JSONException {
// For now we just have one permission, so things can be kept simple...
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
cordova.startActivityForResult(this, imagePickerIntent, 0);
} else {
// Tell the JS layer that something went wrong...
callbackContext.error("Permission denied");
}
}
*/
}