如何在javascript中调用手机插件

时间:2012-09-17 08:44:15

标签: javascript cordova cordova-2.0.0

好的我正在尝试实现一个手机间隙插件,它由两部分组成。我正在使用cordova 2.0.0和eclipse。

这是java部分:

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

}

这是javascript部分:

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

我试图用另一个页面上的另一个javascript函数调用它,但没有成功。我将图像的锚点隐藏在画布上,然后是这一行:

setTimeout(takeScreenShot,500);

编辑 - 在Simon MacDonald回答之后做出的 这与javascript函数有关:

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

以下java打印:

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

产生以下结果:

/mdt/sdcard/Pictures
screenshot_1347893081276.png

编辑关闭设备并再次打开后,我拍摄的屏幕截图显示,手机似乎缓存了它们,并没有将它们实际存储到所选文件夹中。

我确保我的config.xml和我的android清单具有正确的权限和代码行。有谁看到我出错的地方?

2 个答案:

答案 0 :(得分:2)

您的代码中没有任何地方调用saveScreenshot方法。你的takeScreenShot方法应如下所示:

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

然后屏幕截图应保存在“/ sdcard / Pictures”中。这将适用于:

  1. 您记得在屏幕截图插件的config.xml中添加了一个插件行
  2. 您提到这是在另一个页面上,因此请确保该页面包含screenshot.js的脚本标记。

答案 1 :(得分:0)

您是否尝试过绝对路径?

File sdCard = Environment.getExternalStorageDirectory();
File folder = new File (sdcard.getAbsolutePath() + "Pictures");