由于项目需要从文件中读取数据,然后再显示到ionic中。所以用到了cordova-plugin-file这个插件,记录一下。
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
1. 读取项目目录中的文件 www/data
var path = cordova.file.applicationDirectory + "www/data";
window.resolveLocalFileSystemURL(path,
function (fileSystem) {
var reader = fileSystem.createReader();
reader.readEntries(
function (entries) {
try {
if(entries.length > 0){
$scope.data = [];
$scope.mainSettings = [];
for(var i = 0; i<entries.length; i++){
var entry = entries[i];
$scope.item = {'imgsrc':'images/group_land.png','title':entry.name.substring(0,entry.name.length-5),"fullPath":entry.fullPath,
"class":"color" + Math.round(Math.random()*3+1),"check":false,"colclass":""};
if( (i%3) == 0){
$scope.item.imgsrc = "images/group_land.png";
}else if((i%3) == 1){
$scope.item.imgsrc = "images/building_land.png";
}else if((i%3) == 2){
$scope.item.imgsrc = "images/house_land.png";
$scope.item.colclass = 'rightPadding';
}
$scope.mainSettings.push($scope.item);
}
$ionicLoading.hide();
$scope.$digest();
}
} catch (error) {
$ionicLoading.hide();
alert('error :' + error.message);
}
//for(var i = 0 ; i<entries.length;i++){
// var entry = entries[i];
// alert(JSON.stringify(entry));
// }
//console.log(entries);
},
function (err) {
console.log(err);
alert(err);
}
);
}, function (err) {
console.log(err);
alert(err);
}
);
$scope.readKey = function(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,function(fileSystem){ // success get file system
fileSystem.root.getDirectory('wfbdc_files', { create: false }, function (wfbdcEntry) {
wfbdcEntry.getFile('key.txt', { create: false }, function (keyEntry) {
keyEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
//alert(this.result);
$scope.key = evt.target.result;
$scope.$digest();
};
reader.readAsText(file);
}, onErrorReadFile);
},onLoadFileError);
},onLoadFileError);
})
};
本实例是读取项目中的文件和设备中的文件,SDCARD里面的文件,暂时没有读取成功。
1. 首先,在项目中加入cordova-plugin-file这个插件, 不了解这个插件的话,就点这里
cordova plugin add cordova-plugin-file
别忘记加权限,在config.xml中添加
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
1. 读取项目目录中的文件 www/data
var path = cordova.file.applicationDirectory + "www/data";
window.resolveLocalFileSystemURL(path,
function (fileSystem) {
var reader = fileSystem.createReader();
reader.readEntries(
function (entries) {
try {
if(entries.length > 0){
$scope.data = [];
$scope.mainSettings = [];
for(var i = 0; i<entries.length; i++){
var entry = entries[i];
$scope.item = {'imgsrc':'images/group_land.png','title':entry.name.substring(0,entry.name.length-5),"fullPath":entry.fullPath,
"class":"color" + Math.round(Math.random()*3+1),"check":false,"colclass":""};
if( (i%3) == 0){
$scope.item.imgsrc = "images/group_land.png";
}else if((i%3) == 1){
$scope.item.imgsrc = "images/building_land.png";
}else if((i%3) == 2){
$scope.item.imgsrc = "images/house_land.png";
$scope.item.colclass = 'rightPadding';
}
$scope.mainSettings.push($scope.item);
}
$ionicLoading.hide();
$scope.$digest();
}
} catch (error) {
$ionicLoading.hide();
alert('error :' + error.message);
}
//for(var i = 0 ; i<entries.length;i++){
// var entry = entries[i];
// alert(JSON.stringify(entry));
// }
//console.log(entries);
},
function (err) {
console.log(err);
alert(err);
}
);
}, function (err) {
console.log(err);
alert(err);
}
);
$scope.readKey = function(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,function(fileSystem){ // success get file system
fileSystem.root.getDirectory('wfbdc_files', { create: false }, function (wfbdcEntry) {
wfbdcEntry.getFile('key.txt', { create: false }, function (keyEntry) {
keyEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
//alert(this.result);
$scope.key = evt.target.result;
$scope.$digest();
};
reader.readAsText(file);
}, onErrorReadFile);
},onLoadFileError);
},onLoadFileError);
})
};