一、cordova file文件系统操作插件
这个插件实现了一个文件API,允许对设备上的文件进行读/写访问。文件的插件允许你做一个临时的为你的应用程序的存储位置或持续像存储文件的东西(沙箱存储)和存储文件的其他依赖平台的位置。
这个插件是基于几种规格,包括:HTML5文件的API 参考:http://www.cnblogs.com/tianma3798/p/6439258.html
目录和系统扩展新的: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/<app-id>/ | 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 | |
<sdcard>/ | externalRootDirectory | sdcard | r/w | Yes | No | No |
Android/data/<app-id>/ | externalApplicationStorageDirectory | - | r/w | Yes | No | No |
cache | externalCacheDirectry | cache-external | r/w | Yes | No** | No |
files | externalDataDirectory | files-external | r/w | Yes | No | No |
Android配置存储文件位置:Internal 内部存储,Compatibility存储卡存储
<preference name="AndroidPersistentFileLocation" value="Internal" /><preference name="AndroidPersistentFileLocation" value="Compatibility" />配置文件config.xml,设置目录
可用的文件系统的设置可以配置每台。iOS和Android在config.xml文件系统识别的名字要安装一个标签。默认情况下,所有文件系统根已启用。
<preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" /><preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />Android
files
: The application's internal file storage directoryfiles-external
: The application's external file storage directorysdcard
: The global external file storage directory (this is the root of the SD card, if one is installed). You must have theandroid.permission.WRITE_EXTERNAL_STORAGE
permission to use this.cache
: The application's internal cache directorycache-external
: The application's external cache directoryassets
: The application's bundle (read-only)root
: The entire device filesystemAndroid 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 viacordova-plugin-file-transfer
and play it viacordova-plugin-media
.*Note: See Where to Store Files, File System Layouts and Configuring the Plugin for more details about available fs roots.
To use
cdvfile
as a tag'src
you can convert it to native path viatoURL()
method of the resolved fileEntry, which you can get viaresolveLocalFileSystemURL
- see examples below.You can also use
cdvfile://
paths directly in the DOM, for example:<img src="cdvfile://localhost/persistent/img/logo.png" />
Note: This method requires following Content Security rules updates:
Addcdvfile:
scheme toContent-Security-Policy
meta tag of the index page, e.g.:<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap:
cdvfile:https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
Add<access origin="cdvfile://*" />
toconfig.xml
.Converting cdvfile:// to native 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();
更多:
Apache Cordova开发环境搭建(二)VS Code
cordova-plugin-vibration 设备震动整理
cordova-plugin-device 获取设备信息整理
新闻热点
疑难解答