Last active
December 12, 2015 00:58
-
-
Save macdonst/4687773 to your computer and use it in GitHub Desktop.
Gallery Plugin example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cordova.define("cordova/plugin/gallery", | |
function(require, exports, module) { | |
var exec = require("cordova/exec"); | |
var Gallery = function () {}; | |
Gallery.prototype.add = function(imagePath,success,fail) { | |
exec(success,fail,"Gallery","add",[imagePath]); | |
}; | |
Gallery.prototype.remove = function(imagePath,success,fail) { | |
exec(success,fail,"Gallery","remove",[imagePath]); | |
}; | |
var gallery = new Gallery(); | |
module.exports = gallery; | |
}); | |
if (!window.plugins) { | |
window.plugins = {}; | |
} | |
if (!window.plugins.gallery) { | |
window.plugins.gallery = cordova.require("cordova/plugin/gallery"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<plugin name="Gallery" | |
value="com.simonmacdonald.cordova.plugins.GalleryPlugin"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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