我想创建一个类似媒体播放器的应用。想要列出手机/ SD卡中可用的所有视频,点击它就会开始播放。现在我坚持要显示本地视频文件列表。我尝试使用http://ionicframework.com/docs/v2/native/file/和https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html但无法获取视频列表。
所以,请帮我解决问题。 谢谢
答案 0 :(得分:0)
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 {
results: any;
storageDirectory: string ='';
constructor(public navCtrl: NavController, private filePath: FilePath, public platform: Platform, private transfer: Transfer, private file: File){
this.platform.ready().then(() => {
// code to create a new directory
file.createDir(file.externalDataDirectory, 'newDir', true).then((result)=>{
console.log("Directory newDir created" + result);
// code to list the files and folder in the directory
file.listDir(file.externalDataDirectory,'').then((result)=>{
this.storageDirectory = file.externalDataDirectory;
console.log("this.storageDirectory containnig "+ this.storageDirectory);
console.log("listing taking place here" + file.externalDataDirectory);
this.results=result;
console.log("showing result content"+result);
// code to print the name of files and folder on console as well as on device screen
for(let file of result){
if(file.isDirectory == true){
console.log("Code if its a folder");
let name=file.name;
console.log("File name"+name);
}
else if(file.isFile == true){
console.log("Code if its a file");
let name=file.name;
console.log("File name "+name);
let path=this.storageDirectory+name;
console.log(path);
file.getMetadata(function (metadata) {
let size=metadata.size;
console.log("File size"+size);
})
}
}
/*result will have an array of file objects with
file details or if its a directory: */
});
});
});
}
}

请检查我的运行代码,可以帮助您从内部存储中获取文件和文件夹列表。