我正在使用ionic/cordova來構建移動應用程序。爲了調試應用程序,我想使用文件系統編寫一個日誌文件並將其存儲到設備中(即SD卡中的某個地方)。有什麼方法可以使用phonegap來實現這個功能。使用cordova將日誌文件寫入設備
任何人都可以建議一些有用的鏈接?
我正在使用ionic/cordova來構建移動應用程序。爲了調試應用程序,我想使用文件系統編寫一個日誌文件並將其存儲到設備中(即SD卡中的某個地方)。有什麼方法可以使用phonegap來實現這個功能。使用cordova將日誌文件寫入設備
任何人都可以建議一些有用的鏈接?
把這行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的解釋。
我希望幫助,祝你好運
您可以通過添加插件旁邊在您的應用程序做到這一點:
thanks..but我們如何執行,我們在哪裏需要把它? – vishnu
我更新了我的答案 – user2120121