Search code examples
ionic2cordova-plugins

could not bring image to view were image captured by cordovaplugin


I followed this link Ionic 2 File Plugin usage examples to take picture and store it to a target folder inside my internal memory and i tested it it works good, next i am trying to list out all files inside that folder i done it but i could not bring it to view,

<ion-list>
    <ion-item><img  [src]="file"  *ngFor="let file in myFiles"></ion-item>
  </ion-list>


import { Component } from '@angular/core';

    import { NavController, AlertController, ActionSheetController, ToastController, Platform, LoadingController, Loading } from 'ionic-angular';
    import { Camera } from 'ionic-native';
    import { File } from 'ionic-native';
    import { FilePath } from 'ionic-native';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
    file;
    public base64Image: string;
    myFiles: string[];

 constructor(public navCtrl: NavController,
              public alertCtrl: AlertController){}
    listImage(){
            console.log("list image files");
            var fileSystem = cordova.file.externalRootDirectory;
            var folderpath = fileSystem + "Vanan Audio/";
            console.log("img",folderpath);
            File.listDir(cordova.file.externalRootDirectory, 'vanan Audio').then(
              (allFiles) => {
                // do something
                console.log(allFiles);
                var i =0
                console.log(allFiles[i+1].name);
                for (i = 0; i < allFiles.length; i++) { 
                    console.log(allFiles[i].name);
                    console.log(allFiles[i].nativeURL);

                }
                  this.myFiles = allFiles;
                return allFiles
              }
            ).catch(
              (err) => {
                // do something
              }
            );
        }
  }

now i could take picture and stored it to a folder using external root Directory, i could list out file name using loop, I need to show all images inside that folder to my ionic view

i an getting error statement while i run it on to my device

deviceready has not fired after 5 seconds. cordova.js:1223
Unhandled Promise rejection: Template parse errors:
Can't bind to 'ngForIn' since it isn't a known property of 'div'. ("


    <div [ERROR ->]*ngFor="let file in myFiles">

        <img [src]="file.nativeURL"/>
"): HomePage@28:9.............................

Solution

  • Have an array of file paths in the component:

    myFiles:any[];//assign empty array in constructor
    

    In the File.listDir then (assuming listImage() is in component):

    this.myFiles = allFiles;
    

    In the html,

    <div *ngFor="let file of myFiles">
     <img [src]="file.nativeURL"/>
    </div>