如何在科尔多瓦/ PhoneGap的应用程序截图 [英] How to take a screenshot in cordova/phonegap application

查看:237
本文介绍了如何在科尔多瓦/ PhoneGap的应用程序截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的插件来采取截图在我的科尔多瓦应用程序,但错误正在发生。我真的不知道这个错误是什么,因为我在我的Andr​​oid智能手机和应用程序只有几个街区测试。在浏览器中,同样是发生这个错误:类型错误:无法读取属性拯救中未定义,其中保存就来源于此code:

I am trying to take a screenshot in my cordova application using this plugin, but an error is occuring. I don't really know what the error is, as I am testing it on my android smartphone and the app just blocks. In the browser, the same is happening with this error: TypeError: Cannot read property 'save' of undefined, where 'save' comes from this code:

navigator.screenshot.save(function(error,res){
      if(error){
        console.error(error);
      }else{
        console.log('ok',res.filePath);
      }
    });

PS:也试过 navigator.plugin.screenshot ... navigator.plugins.screenshot window.screenshot window.plugin.screenshot window.plugins.screenshot

PS2:如果插件科尔多瓦CLI与科尔多瓦插件安装,一切正常,插件存在plugins文件夹,是科尔多瓦版本> = 3.0我检查。 0和我的是较新的

P.S.2: I checked if plugin is installed with cordova plugins in cordova CLI and everything is ok, plugin           exists in plugins folder and is for cordova version>=3.0.0 and mine is newer

不过,当然,浏览器是不是真的加载插件,因为这也会发生错误有:无法加载资源:与404(未找到)状态的服务器响应HTTP:/ /localhost:23273/www/cordova_plugins.json 。没有截图被采取,检查我的智能手机。

But of course, the browser isn't really loading the plugin, because this error also occurs there: Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:23273/www/cordova_plugins.json. No screenshot is taken, checked on my smartphone.

推荐答案

我用工作灯,并经历了同样的问题。我的解决办法是修改文件Screenshot.js的德code到:

I'm using Worklight and was going through the same problem. My solution was to change de code of the file Screenshot.js to:

var formats = ['png','jpg'];

function Screenshot() {
}

Screenshot.prototype.save = function (callback,format,quality, filename) {
    format = (format || 'png').toLowerCase();
    filename = filename || 'screenshot_'+Math.round((+(new Date()) + Math.random()));
    if(formats.indexOf(format) === -1){
        return callback && callback(new Error('invalid format '+format));
    }
    quality = typeof(quality) !== 'number'?100:quality;
    cordova.exec(function(res){
        callback && callback(null,res);
    }, function(error){
        callback && callback(error);
    }, "Screenshot", "saveScreenshot", [format, quality, filename]);
};

Screenshot.install = function () {
      if (!window.plugins) {
        window.plugins = {};
      }

      window.plugins.screenshot = new Screenshot();
      return window.plugins.screenshot;
    };

cordova.addConstructor(Screenshot.install); 

这样我可以用下面的code呼叫:

This way I can make the call with the following code:

window.plugins.screenshot.save(function(error,res){
          if(error){
            alert(error);
          }else{
            alert('ok',res.filePath); //should be path/to/myScreenshot.jpg
          }
        },'jpg',50,'myScreenShot');

这工作完全在我的Andr​​oid智能手机。

This worked perfectly on my Android smartphone.

我还添加了RES / XML / config.xml文件:

I also added in res / xml / config.xml file:

<feature name="Screenshot">
    <param name="android-package" value="org.apache.cordova.screenshot.Screenshot"/>
</feature>

在AndroidManifest.xml文件:

In the AndroidManifest.xml file:

<uses-permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" />

和下面的包中添加Java类:org.apache.cordova.screenshot.Screenshot

And added the java class in the following package: org.apache.cordova.screenshot.Screenshot

所有这些配置在插件的plugin.xml文件中的信息

All these configurations have the information in the plugin.xml file of the plugin

这篇关于如何在科尔多瓦/ PhoneGap的应用程序截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆