在离子2中列出android文件系统的文件和文件夹 [英] listing of files and folders of android file system in ionic 2
问题描述
我正在尝试列出移动设备外部存储空间中存在的所有文件和文件夹,但无法获得所需的结果。我从代码中获取了帮助,但它没有识别窗口对象。任何人都可以为此提供帮助。
getFiles(path){window.resolveLocalFileSystemURL( path,function(fileSystem){var reader = fileSystem.createReader(); reader.readEntries(function(entries){console.log(entries);},function(err){console.log(err);});} ,function(err){console.log(err);});
我使用了filepath插件,但结果实际上并不是我想要的。在此输入图片说明,我的代码也是
从'@ angular / core'导入{Component};从'ionic-angular'导入{NavController};导入来自'@ ionic-native / transfer'的{Transfer,TransferObject};从'@ ionic-native / file'导入{File};从'@ ionic-native / file-path'导入{FilePath};导入{Platform} 'ionic-angular';声明var cordova:any; @Component({selector:'page-contact',templateUrl:'contact.html',providers:[FilePath,Transfer,TransferObject,File]})export class ContactPage {storageDirectory :string ='';构造函数(public navCtrl:NavController,private filePath:FilePath,public platform:Platform,private transfer:Transfer,private file:File){this.platform.ready()。then(()=> {if(this.platform。是('android')){this.storageDirectory = cordova.file.externalDataDirectory; console.log(this.storageDirectory);} else {//否则退出,但你可以在这里添加更多类型,例如Windows return false;}}) ; this.filePath.resolveNativePath(this.storageDirectory +'files')。then(filePath => console.log(这是我的文件路径+ filePath))。catch(err => console.log(err)); }
这甚至不是在控制台上打印文件名以及如何我在设备屏幕上显示文件内容,请指导我。
从'@ angular / core'导入{Component};从'ionic-angular'导入{NavController};从'@ ionic-native / transfer'导入{Transfer,TransferObject}; import {来自'@ ionic-native / file'的文件};从'@ ionic-native / file-path'导入{FilePath};从'ionic-angular'导入{Platform}; @ Component({selector:'page-contact' ,templateUrl:'contact.html',providers:[FilePath,Transfer,TransferObject,File]})export class ContactPage {storageDirectory:string ='';构造函数(public navCtrl:NavController,private filePath:FilePath,public platform:Platform,private transfer:Transfer,private file:File){this.platform.ready()。then(()=> {file.listDir(file。 externalDataDirectory,'')。then((result)=> {console.log(result); / * result将包含一个包含文件详细信息的文件对象数组,或者是一个目录* / for(let file of result){ if(file.isDirectory == true&& file.name!='。'&& file.name!='..'){//代码如果是文件夹} else if(file.isFile = = true){//代码,如果它的文件名为name = file.name //文件名let path = file.path //文件路径file.getMetadata(function(metadata){let size = metadata.size; // Get文件大小})}}}})}}
你应该使用 @ ionic-native / file documentation ,可以让您更深入地了解不同的本土e脚本。
i am trying to list all the files and folder present at the external storage of mobile, but unable to get the desired result. i took help from the code but it is not recognising the window object. can anyone help for this.
getFiles(path){
window.resolveLocalFileSystemURL(path,
function (fileSystem) {
var reader = fileSystem.createReader();
reader.readEntries(
function (entries) {
console.log(entries);
},
function (err) {
console.log(err);
}
);
}, function (err) {
console.log(err);
}
);
I used the filepath plugin but result is not actually what i want.enter image description here also my code is
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {Transfer, TransferObject} from '@ionic-native/transfer';
import {File} from '@ionic-native/file';
import { FilePath } from '@ionic-native/file-path';
import {Platform} from 'ionic-angular';
declare var cordova:any;
@Component({
selector: 'page-contact',
templateUrl: 'contact.html',
providers: [FilePath, Transfer, TransferObject, File]
})
export class ContactPage {
storageDirectory: string = '';
constructor(public navCtrl: NavController, private filePath: FilePath, public platform: Platform, private transfer: Transfer, private file: File){
this.platform.ready().then(() => {
if(this.platform.is('android')) {
this.storageDirectory = cordova.file.externalDataDirectory;
console.log(this.storageDirectory);
}
else {
// exit otherwise, but you could add further types here e.g. Windows
return false;
}
});
this.filePath.resolveNativePath(this.storageDirectory+'files')
.then(filePath => console.log("this is my file path" + filePath))
.catch(err => console.log(err));
}
}
this is not even printing file name on console and how i display the file content on the device screen, please guide me.
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {Transfer, TransferObject} from '@ionic-native/transfer';
import {File} from '@ionic-native/file';
import { FilePath } from '@ionic-native/file-path';
import {Platform} from 'ionic-angular';
@Component({
selector: 'page-contact',
templateUrl: 'contact.html',
providers: [FilePath, Transfer, TransferObject, File]
})
export class ContactPage {
storageDirectory: string = '';
constructor(public navCtrl: NavController, private filePath: FilePath, public platform: Platform, private transfer: Transfer, private file: File){
this.platform.ready().then(() => {
file.listDir(file.externalDataDirectory,'').then((result)=>{
console.log(result);
/*result will have an array of file objects with
file details or if its a directory*/
for(let file of result){
if(file.isDirectory == true && file.name !='.' && file.name !='..'){
// Code if its a folder
}else if(file.isFile == true){
// Code if its a file
let name=file.name // File name
let path=file.path // File path
file.getMetadata(function (metadata) {
let size=metadata.size; // Get file size
})
}
}
}
})
}
}
You should use the @ionic-native/file documentation that should provide you more insight on the usage of the different native scripts.
这篇关于在离子2中列出android文件系统的文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!