Skip to content

Instantly share code, notes, and snippets.

@macdonst
Last active December 12, 2015 00:58
Show Gist options
  • Save macdonst/4687773 to your computer and use it in GitHub Desktop.
Save macdonst/4687773 to your computer and use it in GitHub Desktop.
Gallery Plugin example
function addpic() {
window.plugins.gallery.add("file:///sdcard/image.png", function() {
console.log("pic added");
}, function() {
console.log("error adding");
});
};
function delpic() {
window.plugins.gallery.remove("file:///sdcard/image.png", function() {
console.log("pic removed");
}, function() {
console.log("error removing");
});
};
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
final CallbackContext cbContext = callbackContext;
String imagePath = args.optString(0);
if ("".equals(imagePath)) {
Log.d(LOG_TAG, "No image path passed in");
cbContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, 0));
return true;
}
imagePath = stripFileProtocol(imagePath);
if (action.equals("add")) {
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
cbContext.sendPluginResult(result);
MediaScannerConnection.scanFile(this.cordova.getActivity(),
new String[] { imagePath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
cbContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, uri.toString()));
}
});
return true;
} else if (action.equals("remove")) {
File fp = new File(imagePath);
fp.delete();
try {
this.cordova.getActivity().getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media.DATA + " = ?",
new String[] { imagePath });
} catch (UnsupportedOperationException t) {
// don't do anything
}
cbContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
return true;
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
return false;
}
<script type="text/javascript" charset="utf-8"
src="gallery.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment