Cordove File实例Demo(二)
window.resolveLocalFileSystemURL(folder,successCB,errorCB)
获取系统目录/文件的操作实例FileEntry,DirectoryEntry
folder:路径file:///协议的目录位置或文件位置,可以自己拼接,可以使用cordova.file中定义的路径,示例:file:///storage/emulated/0/Android/data/
cordova中FileEntry多了个nativeURL,为当前系统中的位置。
使用实例:
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dirEntry) { console.log('file system open: ' + dirEntry.name); var isAppend = true; createFile(dirEntry, "fileToAppend.txt", isAppend); }, onErrorLoadFs);1.读取系统目录内容:
//获取当前程序存储目录$scope.show3 = function () { $scope.folder = cordova.file.applicationStorageDirectory; console.info(cordova.file.applicationStorageDirectory); console.info($scope.folder);}//外部存储目录$scope.show4 = function () { $scope.folder = cordova.file.externalApplicationStorageDirectory;}//获取系统,指定文件夹内容$scope.show2 = function () { console.info($scope.folder); //显示根目录内容 window.resolveLocalFileSystemURI($scope.folder, function (dirEntry) { //显示根目录下的内容 var dirReader = dirEntry.createReader(); var entries = []; var readEntries = function () { //返回FileEntry数组 dirReader.readEntries(function (results) { if (!results.length) { showEntries(entries.sort()); } else { entries = entries.concat(toArray(results)); readEntries(); } }, errorHandler); } readEntries(); }); function errorHandler(err) { console.info(err); } function toArray(list) { return Array.PRototype.slice.call(list || [], 0); } //显示读取结果 function showEntries(entries) { console.info(entries); }}2.下载图片到程序的系统目录$scope.download1 = function () { //如果目录不存在异常:Wrong type for parameter "uri" of resolveLocalFileSystemURI: Expected String, but got Undefined. window.resolveLocalFileSystemURI($scope.folder, function (dirEntry) { getSampleFile(dirEntry); }, errorHandle); //下载图片到程序 function getSampleFile(dirEntry) { console.info(dirEntry); //1.请求png 图片 var xhr = new xmlHttpRequest(); xhr.open('GET', 'http://cordova.apache.org/static/img/cordova_bot.png', true); xhr.responseType = 'blob'; xhr.onload = function () { if (this.status == 200) { var blob = new Blob([this.response], { type: 'image/png' }); //保存 saveFile(dirEntry, blob, 'downloadImage.png'); } } xhr.send(); } //保存到本地 function saveFile(dirEntry, blob, filename) { console.info(blob); //创建文件 dirEntry.getFile(filename, { create: true }, function (fileEntry) { fileEntry.createWriter(function (fileWriter) { fileWriter.onwriteend = function () { console.log('写入文件成功'); if (blob.type == 'image/png') { readBinaryFile(fileEntry); } else { console.error(blob); } } fileWriter.onerror = function (err) { console.error('写入文件失败:' + err.toString()); } fileWriter.write(blob); }); }, errorHandle); } //读取二进制文件 function readBinaryFile(fileEntry) { fileEntry.file(function (file) { var reader = new FileReader(); reader.onloadend = function () { //显示图片文件 console.info(fileEntry.nativeURL); var blob = new Blob([new Uint8Array(reader.result)], { type: 'image/png' }); $scope.imgOne = window.URL.createObjectURL(blob); $scope.$apply(); } reader.readAsArrayBuffer(file); }, errorHandle); //直接显示图片 $scope.imgTwo = fileEntry.toURL(); $scope.$apply(); }}更多:cordova-plugin-file 文件操作整理(二)
cordova-plugin-file 文件操作整理(一)
cordova-plugin-vibration 设备震动整理
http://www.hangge.com/blog/cache/detail_1179.html
新闻热点
疑难解答