gpt4 book ai didi

java - 调用 phonegap 插件时出现 JSON 错误。

转载 作者:行者123 更新时间:2023-11-30 09:32:55 25 4
gpt4 key购买 nike

我在开发iphone/ipad app的时候使用了ios的截图插件。我现在正在创建该应用程序的 android 版本,并正在尝试实现该插件的 android 版本。

插件的 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 {
private PluginResult result = null;

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
// starting on ICS, some WebView methods
// can only be called on UI threads
super.cordova.getActivity().runOnUiThread(new Runnable() {
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);
result = new PluginResult(PluginResult.Status.OK);

} catch (IOException e) {
result = new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage());
}
}
});

// waiting ui thread to finish
while (this.result == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignoring exception, since we have to wait
// ui thread to finish
}
}

return this.result;
}

}

我的 Screenshot.js 看起来像这样:

(function() {
/* Get local ref to global PhoneGap/Cordova/cordova object for exec function.
- This increases the compatibility of the plugin. */
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks

/**
* This class exposes the ability to take a Screenshot to JavaScript
*/
function Screenshot() { }

/**
* Save the screenshot to the user's Photo Library
*/
Screenshot.prototype.saveScreenshot = function() {
cordovaRef.exec(null, null, "Screenshot", "saveScreenshot", []);
};

if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.screenshot) {
window.plugins.screenshot = new Screenshot();
}

})(); /* End of Temporary Scope. */

现在我尝试使用以下代码调用我的 screenshot.js 函数:

function takeScreenShot() {
cordovaRef.exec("Screenshot.saveScreenshot");
}

但是我得到的只是 JSON 错误,我知道我在某个地方要求将它从 Java 字符串转换为 JSON,但我就是不知道如何更改它。好吧,我认为那是错误的......

我的错误是这样的:

ERROR: org.json.JSONException: Value undefined of type java.lang.String cannot be converted to JSONArray.
Error: Status=8 Message=JSON error
file:///android_asset/www/cordova-2.0.0.js: Line 938 : Error: Status=8 Message=JSON error
Error: Status=8 Message=JSON error at file:///android_asset_/www/cordova-2.0.0.js:938

谁能指导我哪里出错了?

最佳答案

问题解决者:

Phonegap Screenshot plugin in Cordova 2.0.0

Simon MacDonald 提供的答案。

关于java - 调用 phonegap 插件时出现 JSON 错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12427555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com