supriya chauhan
supriya chauhan

Reputation: 319

listing of files and folders of android file system in ionic 2

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.

Upvotes: 2

Views: 7772

Answers (2)

vinod kumar
vinod kumar

Reputation: 17

Home.Html

List Directory

<ion-list>
  <ion-item *ngFor="let printValues of showFile">
  {{printValues}}
</ion-item>

Upvotes: 0

Isaac Obella
Isaac Obella

Reputation: 2643

   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.

Upvotes: 1

Related Questions