Skip to content
This repository
Browse code

Updating Screenshot plugin to 2.0.0

  • Loading branch information...
commit 88a57e1c232f604f73be5bd82868d711ac235424 1 parent 4e39187
macdonst macdonst authored
0  ...Screenshot/src/org/apache/cordova/Screenshot.java → ...hot/v1.8.1/src/org/apache/cordova/Screenshot.java
File renamed without changes
0  Android/Screenshot/www/Screenshot.js → Android/Screenshot/v1.8.1/www/Screenshot.js
File renamed without changes
62 Android/Screenshot/v2.0.0/src/org/apache/cordova/Screenshot.java
... ... @@ -0,0 +1,62 @@
  1 +/**
  2 + * Copyright (C) 2012 30ideas (http://30ide.as)
  3 + * MIT licensed
  4 + *
  5 + * @author Josemando Sobral
  6 + * @created Jul 2nd, 2012.
  7 + */
  8 +package org.apache.cordova;
  9 +
  10 +import java.io.File;
  11 +import java.io.FileOutputStream;
  12 +import java.io.IOException;
  13 +
  14 +import org.apache.cordova.api.Plugin;
  15 +import org.apache.cordova.api.PluginResult;
  16 +import org.json.JSONArray;
  17 +
  18 +import android.graphics.Bitmap;
  19 +import android.os.Environment;
  20 +import android.view.View;
  21 +
  22 +public class Screenshot extends Plugin {
  23 +
  24 + @Override
  25 + public PluginResult execute(String action, JSONArray args, String callbackId) {
  26 + // starting on ICS, some WebView methods
  27 + // can only be called on UI threads
  28 + final Plugin that = this;
  29 + final String id = callbackId;
  30 + super.cordova.getActivity().runOnUiThread(new Runnable() {
  31 + //@Override
  32 + public void run() {
  33 + View view = webView.getRootView();
  34 +
  35 + view.setDrawingCacheEnabled(true);
  36 + Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
  37 + view.setDrawingCacheEnabled(false);
  38 +
  39 + try {
  40 + File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
  41 + if (!folder.exists()) {
  42 + folder.mkdirs();
  43 + }
  44 +
  45 + File f = new File(folder, "screenshot_" + System.currentTimeMillis() + ".png");
  46 +
  47 + FileOutputStream fos = new FileOutputStream(f);
  48 + bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
  49 + that.success(new PluginResult(PluginResult.Status.OK), id);
  50 +
  51 + } catch (IOException e) {
  52 + that.success(new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage()), id);
  53 + }
  54 + }
  55 + });
  56 +
  57 + PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
  58 + result.setKeepCallback(true);
  59 + return result;
  60 + }
  61 +
  62 +}
35 Android/Screenshot/v2.0.0/www/Screenshot.js
... ... @@ -0,0 +1,35 @@
  1 +/*
  2 + * This code is adapted from the work of Michael Nachbaur
  3 + * by Simon Madine of The Angry Robot Zombie Factory
  4 + * - Converted to Cordova 1.6.1 by Josemando Sobral.
  5 + * - Converted to Cordova 2.0.0 by Simon MacDonald
  6 + * 2012-07-03
  7 + * MIT licensed
  8 + */
  9 +
  10 +cordova.define("cordova/plugin/screenshot", function(require, exports, module) {
  11 + var exec = require('cordova/exec');
  12 +
  13 + /**
  14 + * This class exposes the ability to take a Screenshot to JavaScript
  15 + */
  16 + var Screenshot = function() {};
  17 +
  18 + /**
  19 + * Save the screenshot to the user's Photo Library
  20 + */
  21 + Screenshot.prototype.saveScreenshot = function() {
  22 + exec(null, null, "Screenshot", "saveScreenshot", []);
  23 + };
  24 +
  25 + var screenshot = new Screenshot();
  26 + module.exports = screenshot;
  27 +
  28 +});
  29 +
  30 +if (!window.plugins) {
  31 + window.plugins = {};
  32 +}
  33 +if (!window.plugins.screenshot) {
  34 + window.plugins.screenshot = cordova.require("cordova/plugin/screenshot");
  35 +}

7 comments on commit 88a57e1

vivianjayakaran

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
Collaborator

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

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

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

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

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

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

Please sign in to comment.
Something went wrong with that request. Please try again.