Trying to implement a phone gap plugin with two parts using cordova 2.0.0 and eclipse.
The java part is as follows:
package org.apache.cordova;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import android.graphics.Bitmap;
import android.os.Environment;
import android.view.View;
public class Screenshot extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
// Code for taking a screenshot...
}
}
The javascript section is as follows:
cordova.define("cordova/plugin/screenshot", function(require, exports, module) {
var exec = require('cordova/exec');
/**
* This class exposes the ability to take a Screenshot to JavaScript
*/
var Screenshot = function() {};
/**
* Save the screenshot to the user's Photo Library
*/
Screenshot.prototype.saveScreenshot = function() {
exec(null, null, "Screenshot", "saveScreenshot", []);
};
var screenshot = new Screenshot();
module.exports = screenshot;
});
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.screenshot) {
window.plugins.screenshot = cordova.require("cordova/plugin/screenshot");
}
Attempting to call this using another JavaScript function on a different page, but encountering issues. Tried hiding anchors of an image on a canvas, then calling the following line:
setTimeout(takeScreenShot,500);
EDIT: Made changes after Simon MacDonald's response. The related JavaScript function is:
function takeScreenShot() {
window.plugins.screenshot.saveScreenshot();
}
The Java code prints:
System.out.println(folder);
System.out.println("screenshot_" + System.currentTimeMillis() + ".png");
Output:
/mdt/sdcard/Pictures
screenshot_1347893081276.png
EDIT: After restarting the device, the screenshots I took appeared, indicating they were cached instead of being stored in the selected folder. Checked config.xml and android manifest for correct permissions and lines of code. Any assistance appreciated.