Added the ability to return errors in resizeImagesTask, so that failed attempts to import will call return as errors, differently than cancelling. Also some whitespace fixing.

This commit is contained in:
CSullivan102
2014-09-04 09:27:22 -04:00
parent 6e3437bac2
commit 9ed29afefd
2 changed files with 41 additions and 30 deletions

View File

@@ -483,6 +483,8 @@ public class MultiImageChooserActivity extends Activity implements OnItemClickLi
private class ResizeImagesTask extends AsyncTask<Set<Entry<String, Integer>>, Void, ArrayList<String>> {
private Exception asyncTaskError = null;
@Override
protected ArrayList<String> doInBackground(Set<Entry<String, Integer>>... fileSets) {
Set<Entry<String, Integer>> fileNames = fileSets[0];
@@ -543,6 +545,7 @@ public class MultiImageChooserActivity extends Activity implements OnItemClickLi
return al;
} catch(IOException e) {
try {
asyncTaskError = e;
for (int i = 0; i < al.size(); i++) {
URI uri = new URI(al.get(i));
File file = new File(uri);
@@ -559,19 +562,24 @@ public class MultiImageChooserActivity extends Activity implements OnItemClickLi
@Override
protected void onPostExecute(ArrayList<String> al) {
Intent data = new Intent();
if (al.size() > 0) {
if (asyncTaskError != null) {
Bundle res = new Bundle();
res.putString("ERRORMESSAGE", asyncTaskError.getMessage());
data.putExtras(res);
setResult(RESULT_CANCELED, data);
} else if (al.size() > 0) {
Bundle res = new Bundle();
res.putStringArrayList("MULTIPLEFILENAMES", al);
if (imagecursor != null) {
res.putInt("TOTALFILES", imagecursor.getCount());
}
data.putExtras(res);
setResult(RESULT_OK, data);
} else {
setResult(RESULT_CANCELED, data);
}
progress.dismiss();
finish();
}

View File

@@ -17,53 +17,56 @@ import android.content.Intent;
import android.util.Log;
public class ImagePicker extends CordovaPlugin {
public static String TAG = "ImagePicker";
private CallbackContext callbackContext;
private JSONObject params;
public static String TAG = "ImagePicker";
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);
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;
int desiredHeight = 0;
int quality = 100;
if (this.params.has("maximumImagesCount")) {
max = this.params.getInt("maximumImagesCount");
max = this.params.getInt("maximumImagesCount");
}
if (this.params.has("width")) {
desiredWidth = this.params.getInt("width");
desiredWidth = this.params.getInt("width");
}
if (this.params.has("height")) {
desiredWidth = this.params.getInt("height");
desiredWidth = this.params.getInt("height");
}
if (this.params.has("quality")) {
quality = this.params.getInt("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);
}
if (this.cordova != null) {
this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0);
}
}
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) {
JSONArray res = new JSONArray();
this.callbackContext.success(res);
} else {
this.callbackContext.error("No images selected");
}
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("An error occurred loading one of the selected images");
} else if (resultCode == Activity.RESULT_CANCELED) {
JSONArray res = new JSONArray();
this.callbackContext.success(res);
} else {
this.callbackContext.error("No images selected");
}
}
}