corodva操作文件类 目录浏览

#cordova-plugin-media 录音并打包上传

https://www.jianshu.com/p/e07f38434ffd


三、目录结构简单整理

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


创建目录

var directoryEntry = fileSystem.root;
           //创建目录
 directoryEntry.getDirectory("MyDirectory", {create: true, exclusive: false}, onDirectorySuccess, onDirectoryFail);
创建文件
   //创建文件
    createFile :function() {
        //获取系统类型
        var type = window.TEMPORARY;
        var size = 5*1024*1024;
        //成功时获取的回调涵数
        window.requestFileSystem(type, size, successCallback, errorCallback)
        
        function successCallback(fs) {
           //开始读取文件 
           fs.root.getFile('log.txt', {create: true, exclusive: true}, function(fileEntry) {
              alert('File creation successfull!')
           }, errorCallback);
        }
     
        function errorCallback(error) {
           alert("ERROR: " + error.code)
        }
         
     },

路径操作:(获取Entry/转换路径)

1、requestFileSystem:仅两个目录 PERSISTENT / TEMPORARY

window.requestFileSystem(存储类型,期望存储空间大小(b字节),成功回调,失败回调)
存储类型:LocalFileSystem.PERSISTENT / LocalFileSystem.TEMPORARY
返回 FileSystem {name: string, root: DirectoryEntry}


window.requestFileSystem(
    LocalFileSystem.PERSISTENT,  //永久目录
    //LocalFileSystem.TEMPORARY,  //临时目录
    0,  //如果是需要创建 PERSISTENT 永久文件 需要为0
    function (fs) {  //fs FileSystem  {name: string, root: DirectoryEntry}
        alert("fs名字:" + fs.name);  //persistent
        alert("DirectoryEntry:"+fs.root);  // DirectoryEntry 对象
        alert("DirectoryEntry isFile:"+fs.root.isFile);  //false
        alert("DirectoryEntry isDirectory:"+fs.root.isDirectory);  //true
        alert("DirectoryEntry name:"+fs.root.name);  //""
        alert("DirectoryEntry fullPath:"+fs.root.fullPath);  // /
        alert("DirectoryEntry fileSystem:"+fs.root.fileSystem);  // undefined
        alert("DirectoryEntry nativeURL:"+fs.root.nativeURL);  // file:///data/data/com.example.hello/files/files/
    },
    function (file_error) {
        alert("错误:" + file_error);
    }
);


2、window.resolveLocalFileSystemURL:可以转换路径(native file <-> cdvfile)

window.resolveLocalFileSystemURL("url", 成功回调, 错误回调);

var native_path = "file:///";
// var cdvfile_path = "cdvfile://localhost/persistent/";
window.resolveLocalFileSystemURL(
    native_path,
    //cdvfile_path,
    function (entry) {
        alert("entry isFile:"+entry.isFile);  //false
        alert("entry isDirectory:"+entry.isDirectory);  //true
        alert("entry name:"+entry.name);  //""
        alert("entry fullPath:"+entry.fullPath);  // /
        alert("entry fileSystem:"+entry.fileSystem);  // undefined
        alert("entry nativeURL:"+entry.nativeURL);  // file:///data/data/com.example.hello/files/files/
        alert('entry toURL: ' + entry.toURL());  // file:///data/data/com.example.hello/files/files/
        alert('entry toInternalURL: ' + entry.toInternalURL());  // cdvfile://localhost/persistent/
    },
    function(file_error){
        alert("错误:" + file_error);
    }
);



开始遍历目录
    readUrl:function(){
        var native_path = cordova.file.externalRootDirectory;
        //cordova.file.dataDirectory
        window.resolveLocalFileSystemURL(native_path, function (entry) {
            var entry = entry;  //通过 requestFileSystem / resolveLocalFileSystemURL 获得
            entry.getDirectory(
                "",
                {create: false},
                function (directory_entry) {
                    directory_entry.createReader().readEntries(  //如果 entry 已经是 DirectoryEntry 可以直接从这步开始
                        function(entry_array){
                            console.log("遍历目录:");
                            for(var index in entry_array){
                                console.log(entry_array[index].toURL());  //native file
                                console.log(entry_array[index].toInternalURL());  //cdvfile
                            }
                        },
                        file_error2
                    );
                },
                file_error
            );

        }, onErrorLoadFs);
        function file_error2 (file_error2) {console.log("遍历错误:" + file_error);}

        function file_error(file_error){}

        function onErrorLoadFs(error){

        }
    },


猜你喜欢

转载自blog.csdn.net/u013040887/article/details/80974078