corodva操作文件类 目录浏览

本文介绍了Cordova在Android平台上如何操作文件系统,包括Android的文件系统布局、创建目录以及使用requestFileSystem和resolveLocalFileSystemURL进行路径操作,如在PERSISTENT和TEMPORARY目录间转换。
#cordova-plugin-media 录音并打包上传

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


三、目录结构简单整理

Android File System Layout

Device Pathcordova.file.*AndroidExtraFileSystemsr/w?persistent?OS clearsprivate
file:///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


创建目录

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){

        }
    },


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值