Skip to content

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.
File renamed without changes.
62 changes: 62 additions & 0 deletions Android/Screenshot/v2.0.0/src/org/apache/cordova/Screenshot.java
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit 88a57e1

Please sign in to comment.