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

Device Pathcordova.file.*AndroidExtraFileSystemsr/w?persistent?OS clearsprivatefile:///android_asset/applicationDirectoryassetsrN/AN/AYes/data/data/<app-id>/applicationStorageDirectory-r/wN/AN/AYes   cachecacheDirectorycacher/wYesYes*Yes   filesdataDirectoryfilesr/wYesNoYes      Documents documentsr/wYesNoYes<sdcard>/externalRootDirectorysdcardr/wYesNoNo   Android/data/<app-id>/externalApplicationStorageDirectory-r/wYesNoNo      cacheexternalCacheDirectrycache-externalr/wYesNo**No      filesexternalDataDirectoryfiles-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 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.

*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 to Content-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://*" /> to config.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 获取设备信息整理

0 0