cordova-plugin-file 文件操作整理(一)
来源:互联网 发布:h5打开淘宝app 编辑:程序博客网 时间:2024/06/05 08:23
一、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
cordova.file.*
AndroidExtraFileSystems
file:///android_asset/
applicationDirectoryassetsrN/AN/AYes/data/data/<app-id>/
applicationStorageDirectory-r/wN/AN/AYes cache
cacheDirectorycacher/wYesYes*Yes files
dataDirectoryfilesr/wYesNoYes Documents
documentsr/wYesNoYes<sdcard>/
externalRootDirectorysdcardr/wYesNoNo Android/data/<app-id>/
externalApplicationStorageDirectory-r/wYesNoNo cache
externalCacheDirectrycache-externalr/wYesNo**No files
externalDataDirectoryfiles-externalr/wYesNoNo更多参考官方文档:http://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html
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 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
.
*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 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:
<img src="cdvfile://localhost/persistent/img/logo.png" />
Note: This method requires following Content Security rules updates:
- Add
cdvfile:
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 获取设备信息整理
- cordova-plugin-file 文件操作整理(一)
- cordova-plugin-file 文件操作整理(二)
- cordova-plugin-file 文件操作整理(三)
- cordova-plugin-file-transfer实现文件上传、下载整理(一)
- cordova-plugin-file-transfer实现文件上传、下载整理(二)
- cordova-plugin-file-transfer 文件上传
- cordova-plugin-file 实例
- cordova-plugin-file 应用
- cordova wifi插件(cordova plugin add cordova-plugin-hotspot)
- cordova-plugin-inappbrowser插件使用整理
- cordova-plugin-themeablebrowser插件使用整理
- cordova-plugin-device 获取设备信息整理
- cordova-plugin-vibration 设备震动整理
- cordova-plugin-statusbar状态栏设置整理
- cordova-plugin-geolocation设备地理位置整理
- cordova-plugin-console插件及Cordova Web页面调试整理
- ionic cordova-plugin-file-transfer插件 Skipping 'cordova-plugin-file' for android
- Cordova 插件开发(plugin)
- LeetCode之路——ZigZag Conversion
- web移动端Fixed在Input获取焦点时ios下产生的BUG及处理
- ionic ——— 下拉刷新及上拉加载
- 第二章简答题图书馆管理信息
- 值不能为 null。参数名: source
- cordova-plugin-file 文件操作整理(一)
- Markdown 用法
- oj刷题--2852二分查找
- 第三章课后简答题
- ibm urbancode example
- 写代码时容易出现的错误(持续更新)
- Java单列设计模式你用对了吗
- Android:关于ViewPager 适配器notifyDataSetChanged不能刷新
- 程序员面试金典-8.反转字串