所有
我可能在Monaca Debugger中发现了一个错误。
我使用以下代码来获取持久存储或临时存储:
window.requestFileSystem(__storageType, _size,
function(_fileSystem) { // Success
logger.info("initFilesystem: _fileSystem.root: " + _fileSystem.root.toURL());
_deferred.resolve(_fileSystem);
}, function(_error) { // Failure
_deferred.reject(_error);
});
在Android和iOS上正确管理文件的位置。 但是,Android和持久存储上的路径不正确,我认为这是Monaca Debugger如何提供cordova插件路径的错误(请注意,我还没有尝试部署到android并在没有调试器的情况下进行验证)。
请参阅下面我获取Android和iOS的路径,其中我只获取Android上的root目录以获取持久性(请注意,在所有示例中,文件都是正确管理的,它只是Android的路径和持久性不正确)。
这是一个错误,还是我需要在代码中管理的功能?
使用的文件对象(简化代码):
function FileIO() {
if (false === (this instanceof FileIO)) { return new FileIO(); } // Enforce that every time a constructor function is called, this function is invoked properly using the new operator.
/** @public @method initFilesystem.
* @desc Initialize filesystem
* @param {boolean} Persistent = true, Temporary = false. Must be boolean. Defaults to true.
* @param {size} Size in bytes requested. Must be number. Defaults to 0 bytes.
* @return Promise (see examples above). */
FileIO.prototype.initFilesystem = function(_persistent, _size) {
/** @private */ var _deferred = new jQuery.Deferred(); // Used for object Promise.
/** @private */ var _err = { code: null };
if (__status != STATUS_INITIALIZED) { // Only perform if not already initialized.
if (typeof _persistent !== 'boolean') { _persistent = true; } // Value must be boolean. Default to true if not set correctly.
if (typeof _size !== 'number') { _size = 0; } // Value must be number. Default to 0 bytes if not set correctly.
(_persistent == true) ? __storageType = window.PERSISTENT : __storageType = window.TEMPORARY;
__status = STATUS_INITIALIZING;
try {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.storageInfo = window.storageInfo || window.webkitStorageInfo;
window.requestFileSystem(__storageType, _size,
function(_fileSystem) { // Success
__fileSystem = _fileSystem;
__status = STATUS_INITIALIZED;
_deferred.resolve(_fileSystem);
}, function(_error) { // Failure
__status = STATUS_NO_FILESYSTEM;
_deferred.reject(_error);
});
} catch (_error) {
__status = STATUS_NO_FILESYSTEM;
_deferred.reject(_error);
}
} else {
_err.code = FILESYSTEM_ALREADY_INITIALIZED;
_deferred.reject(_err);
}
return _deferred.promise();
};
/** @public @method createFile.
* @desc Create file.
* @param {string} (path/)filename. Must be string. Defaults to "file.txt".
* @return Promise (see examples above). */
FileIO.prototype.createFile = function(_fileName) {
/** @private */ var _err = { code: null };
/** @private */ var _deferred = new jQuery.Deferred(); // Used for object Promise.
if (typeof _fileName !== 'string') { _fileName = "file.txt"; } // Default to "file.txt" if not correct.
if (__status != STATUS_NO_FILESYSTEM) { // Object must be initialized.
try {
__fileSystem.root.getFile(_fileName, { create: true, exclusive: true },
function(_fileEntry) { // Success
_deferred.resolve(_fileEntry);
}, function(_error) { // Failure
_deferred.reject(_error);
});
} catch (_error) {
_deferred.reject(_error);
}
} else {
_err.code = FILESYSTEM_NOT_INITIALIZED;
_deferred.reject(_err);
}
return _deferred.promise();
};
/** @public @constructor init.
* @desc Constructor for the class.
* @param N/A.
* @return N/A. */
FileIO.prototype.init = function() {
};
// Construct
this.init();
}
var appFileSystem = new FileIO();
appFileSystem.initFilesystem(true); // Persistent.
appFileSystem.createFile("testfile.txt").done(function(fileEntry) { console.log("createFile: done. fileEntry: "+JSON.stringify(fileEntry)); }).fail(function(error) { console.log("createFile: fail. error.code: "+error.code); });
通过上面的内容,创建FileIO对象,初始化文件系统并创建文件,其中持久存储放在root中(根据最新的Monaca更新测试和验证Cordova 4),而非持久性存储放在右侧目录中