Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
Updating Screenshot plugin to 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Sep 14, 2012
1 parent 4e39187 commit 88a57e1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Android/Screenshot/v2.0.0/src/org/apache/cordova/Screenshot.java
@@ -0,0 +1,62 @@
/**
* Copyright (C) 2012 30ideas (http://30ide.as)
* MIT licensed
*
* @author Josemando Sobral
* @created Jul 2nd, 2012.
*/
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;
}

}
35 changes: 35 additions & 0 deletions Android/Screenshot/v2.0.0/www/Screenshot.js
@@ -0,0 +1,35 @@
/*
* This code is adapted from the work of Michael Nachbaur
* by Simon Madine of The Angry Robot Zombie Factory
* - Converted to Cordova 1.6.1 by Josemando Sobral.
* - Converted to Cordova 2.0.0 by Simon MacDonald
* 2012-07-03
* MIT licensed
*/

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

8 comments on commit 88a57e1

@vivianjayakaran
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Thanks for the updates.
I have added in your latest Screenshot.java and the Screenshot.js into my project.
I am using Cordova 2.0. I have added to screenshot plugin to my config.xml as well.

When I try to call the plugin in the following way:

var win = function() { alert('win!'); }
var fail = function() { alert('fail!'); }
var mp = cordova.require("cordova/plugin/screenshot");
mp.saveScreenshot(win, fail);

I get the error cordova/plugin/screenshot is undefined.

If I change the js code to:

PhoneGap.exec("Screenshot.saveScreenshot");

Then I get the error:

I/System.out(14662): ERROR: org.json.JSONException: Value undefined of type java.lang.String cannot be converted to JSONArray

Is there anything I am missing?
Do you have a sample on how to use it?

Thanks
Vivian

@macdonst
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you remember to include the screenshot.js in your HTML file?

Doing this: PhoneGap.exec("Screenshot.saveScreenshot"); will not work in 2.0.0 or any other Android release I know of.

@vivianjayakaran
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Sorry for the late response. It was an issue with including my JS. I am calling the plugin like this.
PhoneGap.exec(captureWin, captureFail, "Screenshot", "saveScreenshot", []); It works fine.

Thanks for the help :)

@miniviolet
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
I'm using Cordova 2.6.0 and for some reason the lines that uses 'that' don't work and eclipse suggests to "Add cast to 'that'" but I have no idea what they mean by it and so it keeps throwing an error and I can't use the plugin properly.
Do you know what the update from 2.0.0 to 2.6.0 has changed concerning it and how to fix it?

Thank you

@sujeethmenon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using Cordova 2.0.0 and the plugin is not been called . wen i run i get error as cannot call method "select" of undefined at file ..... Please help me . Thanks in advance

@vivianjayakaran
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Sujeeth, Check if you have included the correct cordova.js for Android or for IOS. I'm not sure if there are differences, but from what I have noticed, the js needs to be specific to the platform. Also ensure that you have included the screenshot.js and it is referenced correctly.

@sujeethmenon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Vivian... Issue is been solved . Nw i want to add print plugin to android can you guide me . I have a printer for android and i need to connect to device

Thanks in advance

@AmitLomte1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we capture the screen outside the app using the same plugin

Please sign in to comment.