文件系统 root.fullPath return 未定义和 root.getFile 在 phonegap.js 中给出 "processMessage failed error"

FileSystem root.fullPath return undefined and root.getFile gives "processMessage failed error" in phonegap.js

这是我的第一个问题,很抱歉,如果我的请求写错了。我正在使用 Sencha Touch 2.4.1 和 PhoneGap(我的主要目标是 Android)创建一个项目,使用命令

sencha app build native

打包。简而言之,我需要请求文件系统,然后下载一些文件(图像)。根据 this 之类的帖子,我尝试在开始下载之前使用 "getFile()" 函数,但即使像这样的简单代码也会给我带来麻烦:

Ext.define("MyApp.utils.Globals", {
    singleton: true,
    alias: 'widget.globals',
    config: {
        // Some vars not related to problem
    },
    constructor: function(config) {
        this.initConfig(config);
    },
    initFileSystem: function(quota) {
        if(Ext.browser.is.PhoneGap && !Ext.os.is.Desktop) {
            Ext.device.FileSystem.requestFileSystem({     
                type: window.PERSISTENT,
                size: quota,
                success: this.initFsSuccess,
                failure: this.initFsFailure
            });
        } else {
            window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
            window.requestFileSystem(window.PERSISTENT, quota, this.initFsSuccess, this.initFsFailure);
        }
    },
    initFsSuccess: function(fs) {
        // Save file system reference
        MyApp.app.fS = fs;      
        alert("FS: " + fs.root.fullPath); // This line outputs "FS: undefined"
        fs.root.getFile("newFile.jpg", {create: true, exclusive: false}, function(fileEntry){ alert("GetFile OK"); }, function(error){ alert("GetFile ERR"); });
    },
    initFsFailure: function(err) {
        console.log("File System Error " + err.code);
        Ext.Msg.show({
            title: 'FyleSystem Error',
            message: 'Can\'t access to FileSystem',
            buttons: [ { itemId: 'ok', text: 'Ok', ui: 'decline' } ],
            fn: Ext.emptyFn
        });
    }
});

当我尝试访问文件系统的根完整路径时(如 "initFsSuccess" 函数中的第一个警报),我得到 undefined 作为值。此外,"getFile" 函数停止执行(没有回调被调用,成功或失败)并且在 LogCat 我能够看到这个:

01-12 12:22:20.669: D/CordovaLog(1625): file:///android_asset/www/phonegap.js: Line 1059 : processMessage failed: Error: TypeError: Cannot call method 'error' of undefined
01-12 12:22:20.669: I/Web Console(1625): processMessage failed: Error: TypeError: Cannot call method 'error' of undefined at file:///android_asset/www/phonegap.js:1059
01-12 12:22:20.669: D/CordovaLog(1625): file:///android_asset/www/phonegap.js: Line 1060 : processMessage failed: Stack: TypeError: Cannot call method 'error' of undefined
01-12 12:22:20.669: D/CordovaLog(1625):     at [object Object].getFile (file:///android_asset/www/app.js:1:498661)
01-12 12:22:20.669: D/CordovaLog(1625):     at [object Object].<anonymous> (file:///android_asset/www/app.js:1:617018)
01-12 12:22:20.669: D/CordovaLog(1625):     at file:///android_asset/www/app.js:1:495077
01-12 12:22:20.669: D/CordovaLog(1625):     at file:///android_asset/www/plugins/org.apache.cordova.file/www/requestFileSystem.js:52:25
01-12 12:22:20.669: D/CordovaLog(1625):     at success (file:///android_asset/www/plugins/org.apache.cordova.file/www/fileSystems-roots.js:40:13)
01-12 12:22:20.669: D/CordovaLog(1625):     at Object.callbackFromNative (file:///android_asset/www/phonegap.js:293:54)
01-12 12:22:20.669: D/CordovaLog(1625):     at processMessage (file:///android_asset/www/phonegap.js:1054:21)
01-12 12:22:20.669: D/CordovaLog(1625):     at Function.processMessages (file:///android_asset/www/phonegap.js:1091:13)
01-12 12:22:20.669: D/CordovaLog(1625):     at pollOnce (file:///android_asset/www/phonegap.js:956:17)
01-12 12:22:20.669: D/CordovaLog(1625):     at pollOnceFromOnlineEvent (file:///android_asset/www/phonegap.js:946:5)
01-12 12:22:20.669: I/Web Console(1625): processMessage failed: Stack: TypeError: Cannot call method 'error' of undefined
01-12 12:22:20.669: I/Web Console(1625):     at [object Object].getFile (file:///android_asset/www/app.js:1:498661)
01-12 12:22:20.669: I/Web Console(1625):     at [object Object].<anonymous> (file:///android_asset/www/app.js:1:617018)
01-12 12:22:20.669: I/Web Console(1625):     at file:///android_asset/www/app.js:1:495077
01-12 12:22:20.669: I/Web Console(1625):     at file:///android_asset/www/plugins/org.apache.cordova.file/www/requestFileSystem.js:52:25
01-12 12:22:20.669: I/Web Console(1625):     at success (file:///android_asset/www/plugins/org.apache.cordova.file/www/fileSystems-roots.js:40:13)
01-12 12:22:20.669: I/Web Console(1625):     at Object.callbackFromNative (file:///android_asset/www/phonegap.js:293:54)
01-12 12:22:20.669: I/Web Console(1625):     at processMessage (file:///android_asset/www/phonegap.js:1054:21)
01-12 12:22:20.669: I/Web Console(1625):     at Function.processMessages (file:///android_asset/www/phonegap.js:1091:13)
01-12 12:22:20.669: I/Web Console(1625):     at pollOnce (file:///android_asset/www/phonegap.js:956:17)
01-12 12:22:20.669: I/Web Console(1625):     at pollOnceFromOnlineEvent (file:///android_asset/www/phonegap.js:946:5) at file:///android_asset/www/phonegap.js:1060
01-12 12:22:20.669: D/CordovaLog(1625): file:///android_asset/www/phonegap.js: Line 1061 : processMessage failed: Message: S01 File1432781697 [{"fullPath":"\/","filesystemName":"temporary","isDirectory":true,"nativeURL":"file:\/\/\/mnt\/sdcard\/Android\/data\/com.mysite.MyApp\/cache\/","filesystem":0,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"persistent","isDirectory":true,"nativeURL":"file:\/\/\/mnt\/sdcard\/","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\/com.mysite.MyApp\/files\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"files-external","isDirectory":true,"nativeURL":"file:\/\/\/mnt\/sdcard\/Android\/data\/com.mysite.MyApp\/files\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"documents","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.mysite.MyApp\/files\/Documents\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"sdcard","isDirectory":true,"nativeURL":"file:\/\/\/mnt\/sdcard\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"cache","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.mysite.MyApp\/cache\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"cache-external","isDirectory":true,"nativeURL":"file:\/\/\/mnt\/sdcard\/Android\/data\/com.mysite.MyApp\/cache\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"root","isDirectory":true,"nativeURL":"file:\/\/\/","filesystem":1,"isFile":false,"name":""}]
01-12 12:22:20.669: I/Web Console(1625): processMessage failed: Message: S01 File1432781697 [{"fullPath":"\/","filesystemName":"temporary","isDirectory":true,"nativeURL":"file:\/\/\/mnt\/sdcard\/Android\/data\/com.mysite.MyApp\/cache\/","filesystem":0,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"persistent","isDirectory":true,"nativeURL":"file:\/\/\/mnt\/sdcard\/","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\/com.mysite.MyApp\/files\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"files-external","isDirectory":true,"nativeURL":"file:\/\/\/mnt\/sdcard\/Android\/data\/com.mysite.MyApp\/files\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"documents","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.mysite.MyApp\/files\/Documents\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"sdcard","isDirectory":true,"nativeURL":"file:\/\/\/mnt\/sdcard\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"cache","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.mysite.MyApp\/cache\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"cache-external","isDirectory":true,"nativeURL":"file:\/\/\/mnt\/sdcard\/Android\/data\/com.mysite.MyApp\/cache\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"root","isDirectory":true,"nativeURL":"file:\/\/\/","filesystem":1,"isFile":false,"name":""}] at file:///android_asset/www/phonegap.js:1061

感谢大家的建议,如有需要,我会尽力提供其他infos/specs。

找到解决方案(即使我不知道为什么...)!这就是我所做的:

1) 在我的 "app.js" 文件中,我删除了

'Ext.device.FileSystem'

来自 "requires" 部分的行;

2) 用这个

替换了第一个 post 中报告的 "initFileSystem()" 函数的主体
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.PERSISTENT, quota, this.initFsSuccess, this.initFsFailure);

适用于:

  • Google Chrome v. 39.0.2171.95 m(设备模式开启和关闭);
  • 我的三星 Galaxy S4 (GT-I9515) 运行 官方 Android 4.4.2(作为本机应用程序,不通过浏览器):
  • Android 模拟器(我在 Windows 7 pc 上)运行 Android 4.1.1 与 Intel Atom x86 作为 CPU