我在使用cordova.file
插件从文件系统读取文件时遇到问题。我正在使用以下示例代码:
function onInitFs(fileSystem) {
console.log("filesystem loaded!");
fileSystem.root.getFile(filePath, {}, function (fileEntry) {
console.log("DirectoryEntry.getFile succeeded!");
fileEntry.file(function (file) {
console.log("FileEntry.file succeeded!");
var reader = new FileReader();
reader.onloadend = function (e) {
//file contents are in this.result
};
reader.readAsText(file);
}, console.log);
}, console.log);
}
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onInitFs, console.log);
这是我在输出窗口中看到的错误消息:
D/PluginManager( 2597): init()
E/cutils ( 915): Failed to mkdirat(/storage/sdcard/Android): Read-only file system
W/ContextImpl( 2597): Failed to ensure directory: /storage/sdcard/Android/data/io.cordova.<myappname>/files
W/System.err( 2597): java.lang.NullPointerException
W/System.err( 2597): at org.apache.cordova.file.FileUtils.getAvailableFileSystems(FileUtils.java:132)
W/System.err( 2597): at org.apache.cordova.file.FileUtils.initialize(FileUtils.java:192)
W/System.err( 2597): at org.apache.cordova.PluginEntry.createPlugin(PluginEntry.java:96)
W/System.err( 2597): at org.apache.cordova.PluginManager.startupPlugins(PluginManager.java:195)
W/System.err( 2597): at org.apache.cordova.PluginManager.init(PluginManager.java:106)
W/System.err( 2597): at org.apache.cordova.CordovaWebView.loadUrlIntoView(CordovaWebView.java:457)
W/System.err( 2597): at org.apache.cordova.CordovaWebView.loadUrlIntoView(CordovaWebView.java:444)
W/System.err( 2597): at org.apache.cordova.CordovaWebView.loadUrlIntoView(CordovaWebView.java:541)
W/System.err( 2597): at org.apache.cordova.CordovaWebView.loadUrl(CordovaWebView.java:435)
W/System.err( 2597): at org.apache.cordova.CordovaActivity.loadUrl(CordovaActivity.java:391)
W/System.err( 2597): at io.cordova.<myappname>.onCreate(ApprovalsHybrid.java:33)
W/System.err( 2597): at android.app.Activity.performCreate(Activity.java:5231)
W/System.err( 2597): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
W/System.err( 2597): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
W/System.err( 2597): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
W/System.err( 2597): at android.app.ActivityThread.access$800(ActivityThread.java:135)
W/System.err( 2597): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
W/System.err( 2597): at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err( 2597): at android.os.Looper.loop(Looper.java:136)
W/System.err( 2597): at android.app.ActivityThread.main(ActivityThread.java:5017)
W/System.err( 2597): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 2597): at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err( 2597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
W/System.err( 2597): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
W/System.err( 2597): at dalvik.system.NativeStart.main(Native Method)
I/System.out( 2597): Error adding plugin org.apache.cordova.file.FileUtils.
W/Vold ( 915): Returning OperationFailed - no handler for errno 30
我正在为 VS 和 Android 模拟器使用多设备混合应用程序插件。
更新1:我的config.xml
:
<?xml version="1.0" encoding="utf-8"?>
<widget xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:vs="http://schemas.microsoft.com/appx/2014/htmlapps" id="io.cordova.<myappname>" version="1.0.0.0" xmlns="http://www.w3.org/ns/widgets">
<name><myappname></name>
<description>
A blank project that uses Apache Cordova to help you build an app that targets multiple mobile platforms: Android, iOS, Windows, and Windows Phone.
</description>
<author href="http://cordova.io" email="dev@cordova.apache.org">
Apache Cordova Team
</author>
<content src="index.html" />
<access origin="*" />
<preference name="SplashScreen" value="screen" />
<vs:features>
<vs:feature>org.apache.cordova.globalization@0.2.8</vs:feature>
<vs:feature>org.apache.cordova.file@1.2.0</vs:feature>
</vs:features>
<vs:platformSpecificValues />
</widget>
UPDATE2:cordova.file
好的,我发现插件 v1.2.0存在一些问题。当我将cordova.file
插件版本从 v1.2.0 切换到 v1.1.0 时,我到达了调用成功回调的地步requestFileSystem
。但是getFile
失败了FileNotFoundException
。原因可能是不正确的相对路径。
UPDATE3:修复了不正确的使用readAsText