【发布时间】:2016-03-06 10:13:17
【问题描述】:
我正在使用 ionic/cordova 构建移动应用程序。为了调试应用程序,我想使用文件系统编写一个日志文件并将其存储到设备中(即 SD 卡中的某个位置)。有没有办法使用phonegap来实现这个功能。
谁能推荐一些有用的链接?
【问题讨论】:
我正在使用 ionic/cordova 构建移动应用程序。为了调试应用程序,我想使用文件系统编写一个日志文件并将其存储到设备中(即 SD 卡中的某个位置)。有没有办法使用phonegap来实现这个功能。
谁能推荐一些有用的链接?
【问题讨论】:
您可以通过在您的应用中添加下一个插件来做到这一点:
【讨论】:
放入这一行 log.bat 文件,这将打开控制台并记录设备上发生的每一件事。
adb logcat CordovaActivity:V CordovaWebView:V CordovaWebViewClient:V IceCreamCordovaWebViewClient:V CordovaLog:V *:S
更新: 写入文件: 一个 good example 跟随,但你需要处理错误回调,以获取错误
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dir) {
console.log("got main dir",dir);
dir.getFile("log.txt", {create:true}, function(file) {
console.log("got the file", file);
logOb = file;
writeLog("App started");
});
});
function writeLog(str) {
if(!logOb) return;
var log = str + " [" + (new Date()) + "]\n";
console.log("going to log "+log);
logOb.createWriter(function(fileWriter) {
fileWriter.seek(fileWriter.length);
var blob = new Blob([log], {type:'text/plain'});
fileWriter.write(blob);
console.log("ok, in theory i worked");
}, fail);
}
document.querySelector("#actionOne").addEventListener("touchend", function(e) {
//Ok, normal stuff for actionOne here
//
//Now log it
writeLog("actionOne fired");
}, false);
document.querySelector("#actionTwo").addEventListener("touchend", function(e) {
//Ok, normal stuff for actionTwo here
//
//Now log it
writeLog("actionTwo fired");
}, false);
最后:
function justForTesting() {
logOb.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
console.log(this.result);
};
reader.readAsText(file);
}, fail);
}
这里是source 的解释。
希望对你有帮助,祝你好运
【讨论】:
【讨论】: