简体   繁体   中英

How to list local video files using ionic 2(cordova) like gallery or any media player does

I want to make an app like media player. Want to list all videos available in the phone/sdcard and clicking on it will start playing. Now I am stuck to display list of local video files. I have tried with http://ionicframework.com/docs/v2/native/file/ and https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html but unable to get list of videos.

So, please help me to get resolution. Thanks

 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: */ }); }); }); } } 

Please check this my running code, may can help you to get the list of files and folders from the internal storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM