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

编辑——在西蒙麦克唐纳的回答之后做出的,这与一个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 清单具有正确的权限和代码行。有人看到我哪里出错了吗?

4

2 回答 2

2

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

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

然后屏幕截图应保存在“/sdcard/Pictures”中。这将在以下情况下起作用:

  1. 您记得在 config.xml 中为屏幕截图插件添加了一个插件行
  2. 您提到这是在另一个页面上,因此请确保该页面具有 screenshot.js 的脚本标记。
于 2012-09-17T14:20:11.820 回答
0

您是否尝试过绝对路径?

File sdCard = Environment.getExternalStorageDirectory();
File folder = new File (sdcard.getAbsolutePath() + "Pictures");
于 2012-09-17T15:24:04.657 回答