Question

Ok I am trying to implement an phone gap plugin, that consists of two parts. I am using cordova 2.0.0 and eclipse.

Here is the java part:

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) {
        // starting on ICS, some WebView methods
        // can only be called on UI threads
        final Plugin that = this;
        final String id = callbackId;
        super.cordova.getActivity().runOnUiThread(new Runnable() {
            //@Override
            public void run() {
                View view = webView.getRootView();

                view.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
                view.setDrawingCacheEnabled(false);

                try {
                    File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
                    if (!folder.exists()) {
                        folder.mkdirs();
                    }

                    File f = new File(folder, "screenshot_" + System.currentTimeMillis() + ".png");

                    FileOutputStream fos = new FileOutputStream(f);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    that.success(new PluginResult(PluginResult.Status.OK), id);

                } catch (IOException e) {
                    that.success(new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage()), id);
                }
            }
        });

        PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
        result.setKeepCallback(true);
        return result;
    }

}

Here is the javascript part:

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");
}

I am trying to call this with another javascript function on another page, but without success. I hide the anchors of an image on a canvas, then this line:

setTimeout(takeScreenShot,500);

EDIT -- made after Simon MacDonald's answer this then relates to a javascript function:

function takeScreenShot() {
window.plugins.screenshot.saveScreenshot();
}

The following java prints:

System.out.println(folder);
System.out.println("screenshot_" + System.currentTimeMillis() + ".png");

Produce the following results:

/mdt/sdcard/Pictures
screenshot_1347893081276.png

EDIT After turning the device off and on again, the screenshots I took appeared, the phone seems to cache them, and not actually store them to the selected folder.

I have ensured that my config.xml and my android manifest have the right permissions and lines of code. Anyone see where im going wrong?

Was it helpful?

Solution

nowhere in your code do you ever call the saveScreenshot method. Your takeScreenShot method should look like this:

function takeScreenShot() {
    window.plugins.screenshot.saveScreenshot();
}

Then the screen shot should be saved in "/sdcard/Pictures". This will work if:

  1. You remembered to put a plugin line into your config.xml for the screen shot plugin
  2. You mentioned this is on another page so make sure that page has a script tag for screenshot.js.

OTHER TIPS

Did you try with the Obsolute Path?

File sdCard = Environment.getExternalStorageDirectory();
File folder = new File (sdcard.getAbsolutePath() + "Pictures");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top