[英]How to use the Cordova/Phonegap Filesystem API with Rikulo?
請執行我的英語...我最初嘗試為Android platfrom編寫跨平台Web應用程序。 我必須將文件寫入和讀取到手機的文件系統。 我已經看到軟件包rikulo_gap本身不支持Filesystem API。 Rikulo使用的Fileystem API就是其中一個 ,它當然不適用於移動平台。
因此,我已經看到Rikulo的Js-Package應該使我的Dart-Code與我的Cordova.js-File(v4.0.0)可以互操作,其中包括全局函數requestFileSystem(persistence,size,success-funct,fail-funct)。 現在我試圖使我的代碼正常工作,但是我沒有找回文件系統-我遇到了錯誤...
這是我的Dart代碼:
import 'package:rikulo_gap/device.dart';
import 'dart:js' as js;
void main() {
Device.init().then((device) => onDeviceReady(device)).
catchError((ex) => print("AN ERROR OCCURED: " + ex.toString()));
}
void onDeviceReady(device) {
var persistent = js.context['PERSISTENT'];
print("Should be Constant PERSISTENT of LFS: " + persistent.toString());
var fsName = js.context.callMethod('requestFileSystem',[persistent,0,dofs,fail]);
}
void dofs(fs) {
print("Success");
var myFs = js.context[fs];
print("The Return of RequestFileSystem is a: " + myFs.toString());
}
LogCat說:
10-23 18:08:32.589: D/CordovaLog(951): file:///android_asset/www/adam.dart.js: Line 12111 : Should be Constant PERSISTENT of LFS: 1
10-23 18:08:32.624: D/CordovaLog(951): file:///android_asset/www/adam.dart.js: Line 12111 : Success
10-23 18:08:32.649: D/CordovaLog(951): file:///android_asset/www/cordova.js: Line 1060 : processMessage failed: Stack: Error
10-23 18:08:32.649: D/CordovaLog(951): at dart.wrapException (file:///android_asset/www/adam.dart.js:2520:15)
10-23 18:08:32.649: D/CordovaLog(951): at JsObject.$index (file:///android_asset/www/adam.dart.js:11549:17)
10-23 18:08:32.649: D/CordovaLog(951): at dart.J.$index$asx (file:///android_asset/www/adam.dart.js:19278:41)
10-23 18:08:32.649: D/CordovaLog(951): at dofs (file:///android_asset/www/adam.dart.js:4185:7)
10-23 18:08:32.649: D/CordovaLog(951): at dart.Primitives_applyFunction (file:///android_asset/www/adam.dart.js:2489:23)
10-23 18:08:32.649: D/CordovaLog(951): at _callDartFunction (file:///android_asset/www/adam.dart.js:11464:29)
10-23 18:08:32.649: D/CordovaLog(951): at file:///android_asset/www/adam.dart.js:11671:18
10-23 18:08:32.649: D/CordovaLog(951): at file:///android_asset/www/plugins/org.apache.cordova.file/www/requestFileSystem.js:52:25
10-23 18:08:32.649: D/CordovaLog(951): at success (file:///android_asset/www/plugins/org.apache.cordova.file/www/fileSystems-roots.js:40:13)
10-23 18:08:32.649: D/CordovaLog(951): at Object.cordova.callbackFromNative (file:///android_asset/www/cordova.js:293:54)
10-23 18:08:32.649: D/CordovaLog(951): file:///android_asset/www/cordova.js: Line 1061 : processMessage failed: Message: S01 File1158866550 [{"fullPath":"\/","filesystemName":"temporary","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/Android\/data\/de.htwg.myAndroid\/cache\/","filesystem":0,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"persistent","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"content","isDirectory":true,"nativeURL":"cdvfile:\/\/localhost\/content\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"files","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/de.htwg.myAndroid\/files\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"files-external","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/Android\/data\/de.htwg.myAndroid\/files\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"documents","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/de.htwg.myAndroid\/files\/Documents\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"sdcard","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"cache","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/de.htwg.myAndroid\/cache\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"cache-external","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/Android\/data\/de.htwg.myAndroid\/cache\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"root","isDirectory":true,"nativeURL":"file:\/\/\/","filesystem":1,"isFile":false,"name":""}]
令我煩惱的是,堆棧上的第一個異常是dart.wrapException,所以我想這是一個常見的Dart <> Js Interop問題。
我遵循了Cordova使用Android構建的官方說明,目前我正在使用cordova run android
的Windows CommandLine構建。 (是的,我已經安裝了插件(通過命令行安裝File,FileUtil,Device,並將它們添加到項目中)。
所以有人可以告訴我:
Rikulo還不支持Cordova文件系統API。 您可以貢獻一個,也可以嘗試使用window.requestFileSystem。
返回到dofs的對象是JsObject實例,因此您無需使用js.context
進行轉換。 那是,
var myFs = js.context[fs];
應修訂為
var myFs = fs;
omG,您到了第2點的地方就這么對了……浪費了我兩天的時間:O(第1點無法正常工作,因為使用dart:html程序包,函數window.requestFileSystem調用了webKitRequestFileSystem,該功能僅在chrome瀏覽器和LogCat告訴我類似“不支持功能webKitRequestFileSystem)
我很生氣,因為fs.toString剛返回了[Object object],所以我認為它只是一個空對象,並且因為我只使用了dofs(fs)(無類型)對函數進行了簽名,所以我的IDE / Dart-API沒有給出我為JsObject提出了諸如“ callMethod()”之類的建議...現在我以這種方式進行了更改:
void main() {
Device.init().then((device) => onDeviceReady(device)).catchError((ex) => print("AN ERROR OCCURED: " + ex.toString()));
}
void onDeviceReady(device) {
var persistent = context['PERSISTENT'];
print("Should be Constant PERSISTENT of LFS: " + persistent.toString());
context.callMethod('requestFileSystem',[persistent,0,dofs,fail]);
}
void dofs(JsObject fs) {
print("Success");
var fsroot = fs['root'];
createFile(fsroot);
}
void createFile(JsObject fsroot) {
var cNe = new JsObject.jsify({'create': true, 'exclusive': false});
fsroot.callMethod('getFile', ["DartCreatedFile.txt",cNe,gotFileEntry,fail]);
}
void gotFileEntry(JsObject fileEntry) {
print(fileEntry['fullPath']);
}
Logcat成功地告訴我:
10-24 14:17:03.945: D/CordovaLog(6685): file:///android_asset/www/fileSystemApi.js: Line 8 : Device is Ready!!!
10-24 14:17:04.055: D/CordovaLog(6685): file:///android_asset/www/adam.dart.js: Line 12294 : Should be Constant PERSISTENT of LFS: 1
10-24 14:17:04.085: D/CordovaLog(6685): file:///android_asset/www/adam.dart.js: Line 12294 : Success
10-24 14:17:04.105: D/CordovaLog(6685): file:///android_asset/www/adam.dart.js: Line 12294 : //DartCreatedFile.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.