一、cordova file文件系统操作插件

这个插件实现了一个文件API,允许对设备上的文件进行读/写访问。文件的插件允许你做一个临时的为你的应用程序的存储位置或持续像存储文件的东西(沙箱存储)和存储文件的其他依赖平台的位置。

目录和系统扩展新的:http://www.w3.org/tr/2012/wd-file-system-api-20120417/虽然大部分的插件代码是写在早期的规格是电流:http://www.w3.org/tr/2011/wd-file-system-api-20110419/

它还实现了输出规格:http://dev.w3.org/2009/dap/file-system/file-writer.html

注意:虽然W3C文件规格是过时的Web浏览器,文件系统API在科尔多瓦应用这个插件在支持平台列表中列出的支持平台,随着浏览器平台的例外。

要了解如何使用插件的一些想法,请检查页面底部的示例。额外的例子(浏览器集中),看到HTML5岩文件第。

这个插件定义了全局cordova.file对象。在deviceready事件后,才可用。

二、安装命令

cordova plugin add cordova-plugin-file

三、目录结构简单整理

Android File System Layout

Device Path

cordova.file.*

AndroidExtraFileSystems

r/w?

persistent?

OS clears

private

file:///android_asset/

applicationDirectory

assets

r

N/A

N/A

Yes

/data/data//

applicationStorageDirectory

-

r/w

N/A

N/A

Yes

cache

cacheDirectory

cache

r/w

Yes

Yes*

Yes

files

dataDirectory

files

r/w

Yes

No

Yes

Documents

documents

r/w

Yes

No

Yes

/

externalRootDirectory

sdcard

r/w

Yes

No

No

Android/data//

externalApplicationStorageDirectory

-

r/w

Yes

No

No

cache

externalCacheDirectry

cache-external

r/w

Yes

No**

No

files

externalDataDirectory

files-external

r/w

Yes

No

No 更多参考官方文档:

http://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html

Android配置存储文件位置:Internal 内部存储,Compatibility存储卡存储

配置文件config.xml,设置目录

可用的文件系统的设置可以配置每台。iOS和Android在config.xml文件系统识别的名字要安装一个标签。默认情况下,所有文件系统根已启用。

Android

files: The application's internal file storage directory

files-external: The application's external file storage directory

sdcard: The global external file storage directory (this is the root of the SD card, if one is installed). You must have the android.permission.WRITE_EXTERNAL_STORAGE permission to use this.

cache: The application's internal cache directory

cache-external: The application's external cache directory

assets: The application's bundle (read-only)

root: The entire device filesystem

Android also supports a special filesystem named "documents", which represents a "/Documents/" subdirectory within the "files" filesystem.

四、使用操作说明

1.当目标的WebView的客户(而不是浏览器)或本地应用程序(Windows),你不需要在使用持久性存储使用requestquota。

2.在沙盒目录结构中使用window.requestFileSystem

3.获取或操作系统文件/目录,可以使用resolveLocalFileSystemURL

window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dirEntry) {

console.log('file system open: ' + dirEntry.name);

var isAppend = true;

createFile(dirEntry, "fileToAppend.txt", isAppend);

}, onErrorLoadFs);

五、cdvfile protocal 的使用

支持cdvfile://localhost/persistent|temporary|another-fs-root*/path/to/file can be used for platform-independent file paths. cdvfile paths are supported by core plugins - for example you can download an mp3 file to cdvfile-path via cordova-plugin-file-transfer and play it via cordova-plugin-media.

To use cdvfile as a tag' src you can convert it to native path via toURL() method of the resolved fileEntry, which you can get via resolveLocalFileSystemURL - see examples below.

You can also use cdvfile:// paths directly in the DOM, for example:

Note: This method requires following Content Security rules updates:

Add cdvfile: scheme to Content-Security-Policy meta tag of the index page, e.g.:

Add  to config.xml.

Converting cdvfile:// tonative path

resolveLocalFileSystemURL('cdvfile://localhost/temporary/path/to/file.mp4', function(entry) {

var nativePath = entry.toURL();

console.log('Native URI: ' + nativePath);

document.getElementById('video').src = nativePath;

Converting native path to cdvfile://

resolveLocalFileSystemURL(nativePath, function(entry) {

console.log('cdvfile URI: ' + entry.toInternalURL());

Using cdvfile in core plugins

fileTransfer.download(uri, 'cdvfile://localhost/temporary/path/to/file.mp3', function (entry) { ...

var my_media = new Media('cdvfile://localhost/temporary/path/to/file.mp3', ...);

my_media.play();

更多:

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐