代码之家  ›  专栏  ›  技术社区  ›  Mohamed Wahshey

ionic 3 movefile函数问题

  •  0
  • Mohamed Wahshey  · 技术社区  · 6 年前

    enter image description here 我使用相机本机插件拍照,相机工作得很好,照片正在显示,但当我试图使用文件本机插件(moveFile)方法,只是把拍摄的照片移动到文件目录,而不是缓存,什么都没有发生! 文件和fileError在TS页中导入,文件也在App模块中提供

    这是我的TS(更新版):

    getImageFromCamera() {
    const options: CameraOptions = {
      quality: 20,
      saveToPhotoAlbum: true,
      destinationType: this.camera.DestinationType.FILE_URI,
      sourceType: this.camera.PictureSourceType.CAMERA,
      encodingType: this.camera.EncodingType.JPEG,
      allowEdit: true
    };
    this.camera.getPicture(options).then((imageData) => {
      this.imageUrl = imageData;
      let imageName = imageData.substr(imageData.lastIndexOf('/') + 1);
      this.file.checkDir(this.file.externalRootDirectory, 'DEMO')
        .then(() => {
          this.fileCreated = true;
        }, (err) => {
        });
      if (this.fileCreated) {
      }
      else {
        this.file.createDir(this.file.externalRootDirectory, "DEMO", true)
          .then((res) => {
          }, (err) => {
          });
      }
      let tempPath = this.imageUrl.substr(0, this.imageUrl.lastIndexOf('/') +1);
      let androidPath = this.file.externalRootDirectory + '/DEMO/';
      this.file.moveFile(tempPath, imageName, androidPath, imageName)
        .then((res) => {
          this.file.readAsDataURL(androidPath, imageName)
            .then((res) => {
              this.images.push(res);
            }, (err) => {
              console.log("Error in Image Get Path---");
              console.log(JSON.stringify(err));
            });
        }, (err) => {
        });
      // this.toDataURL(this.imageUrl, function (dataUrl) {
      //  console.log('RESULT:' + dataUrl);
      //  this.images.push(this.imageUrl);
      // });
     }, (err) => {
      console.log(JSON.stringify(err));
     });
    }
    toDataURL(url, callback) {
    let xhr = new XMLHttpRequest();
    xhr.onload = function () {
      let reader = new FileReader();
      reader.onloadend = function () {
        callback(reader.result);
      };
      reader.readAsDataURL(xhr.response);
     };
    xhr.open('GET', url);
    xhr.responseType = 'blob';
    xhr.send();
    }
    

    <ion-item>
      <h5>Choose files to upload</h5>
      <button (tap)="getImageFromCamera()" item-end ion-button round outline 
       color="sideBar">Upload</button>
    </ion-item>
    <ion-slides *ngIf="images.length >= 1">
      <ion-slide style="float: left;" *ngFor="let image of images; let i = 
       index;" class="image-container">
        <ion-icon (tap)="onDeletePhoto(i)" class="icon-container" name="close- 
         circle"></ion-icon>
        <img [src]="image" width="100">
      </ion-slide>
    </ion-slides>
    <ion-item>
    

    有人知道吗?如果您有任何问题,我没有提供答案,请随时问。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Tej Patel    6 年前

    在这里,我已经为您创建了演示,这个代码从相机拍摄照片并保存到文件夹中,然后从本地路径读取图像并显示在屏幕上,我也测试过,对我来说效果很好,现在应该很好。

    主页.ts

    确保已导入所有包

    从“@angular/core”导入{Component};
    从'ionic angular'导入{NavController,ToastController};

    从'@ionic native/File'导入{File};

    export class HomePage {
    
    public imageURI: any;
    public imageName: any;
    public fileCreated: boolean = false;
    public imageString: any;
    resultImageArray: any;
    

    私有文件:文件, 私人摄像机:摄像机, 专用toastCtrl:ToastController,){

    }

    getImageFromCamera() {
    
    
        const options: CameraOptions = {
            quality: 20,
            saveToPhotoAlbum: true,
            destinationType: this.camera.DestinationType.FILE_URI,
            sourceType: this.camera.PictureSourceType.CAMERA,
            encodingType: this.camera.EncodingType.JPEG,
            allowEdit: true
        };
    
        this.camera.getPicture(options).then((imageData) => {
            this.imageURI = imageData;
            this.imageName = imageData.substr(imageData.lastIndexOf('/') + 1);
    
            // Create a folder in memory location
            this.file.checkDir(this.file.externalRootDirectory, 'ImagesDemo')
                .then(() => {
                    this.fileCreated = true;
                }, (err) => {
                    console.log("checkDir: Error");
                    console.log(JSON.stringify(err));
                    this.presentToast("checkDir Failed");
                });
            if (this.fileCreated) {
                this.presentToast("Directory Already exist");
            }
            else {
                this.file.createDir(this.file.externalRootDirectory, "ImagesDemo", true)
                    .then((res) => {
                        this.presentToast("Directory Created");
                    }, (err) => {
                        console.log("Directory Creation Error:");
                        console.log(JSON.stringify(err));
                    });
            }
    
            //FILE MOVE CODE
            let tempPath = this.imageURI.substr(0, this.imageURI.lastIndexOf('/') + 1);
            let androidPath = this.file.externalRootDirectory + '/ImagesDemo/';
            this.imageString = androidPath + this.imageName;
    
            this.file.moveFile(tempPath, this.imageName, androidPath, this.imageName)
                .then((res) => {
                    this.presentToast("Image Saved Successfully");
                    this.readImage(this.imageString);
    
                }, (err) => {
                    console.log("Image Copy Failed");
                    console.log(JSON.stringify(err));
                    this.presentToast("Image Copy Failed");
                });
            //Complete File Move Code
    
            this.toDataURL(this.imageURI, function (dataUrl) {
                console.log('RESULT:' + dataUrl);
            });
        }, (err) => {
            console.log(JSON.stringify(err));
            this.presentToast(JSON.stringify(err));
        });
    }
    
    
    presentToast(msg) {
        let toast = this.toastCtrl.create({
            message: msg,
            duration: 2000
        });
        toast.present();
    }
    
    toDataURL(url, callback) {
        let xhr = new XMLHttpRequest();
        xhr.onload = function () {
            let reader = new FileReader();
            reader.onloadend = function () {
                callback(reader.result);
            };
            reader.readAsDataURL(xhr.response);
        };
        xhr.open('GET', url);
        xhr.responseType = 'blob';
        xhr.send();
    }
    
    
    
    readImage(filePath) {
        let tempPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
        let imageName = filePath.substr(filePath.lastIndexOf('/') + 1);
    
        this.file.readAsDataURL(tempPath, imageName)
            .then((res) => {
                this.presentToast("Image Get Done");
                this.resultImageArray = res;
            }, (err) => {
                this.presentToast("Image Get Error");
            });
    }
    

    主页.html

     <button ion-button   item-end icon-start block  (click)="getImageFromCamera()">Click Here</button>
    
    
      <ion-card *ngIf=resultImageArray>
    <img src="{{resultImageArray}}"/></ion-card>
    

    现在我们准备好了,只要运行到设备中,就可以得到想要的输出。

    我还附加了代码的输出。

    enter image description here

        2
  •  0
  •   Tej Patel    6 年前

    参考这个代码,我已经做了移动图像到指定文件夹,用这个代码。希望这对你有帮助。。
    这是我的密码:

      getImageFromCamera() {
    
        const options: CameraOptions = {
            quality: 20,
            saveToPhotoAlbum: true,
            destinationType: this.camera.DestinationType.FILE_URI,
            sourceType: this.camera.PictureSourceType.CAMERA,
            encodingType: this.camera.EncodingType.JPEG,
            allowEdit: true
        };
    
        this.camera.getPicture(options).then((imageData) => {
    
            this.imageURI = imageData;
            this.imageName = imageData.substr(imageData.lastIndexOf('/') + 1);
    
            // Create a folder 'DEMO' in memory location of device to store image
    
            this.file.checkDir(this.file.externalRootDirectory, 'DEMO')
                .then(() => {
                    this.fileCreated = true;
                }, (err) => {
                    console.log("checkDir: Error");
                });
            if (this.fileCreated) {
                this.presentToast("Directory Already exist");
            }
            else {
                this.file.createDir(this.file.externalRootDirectory, "DEMO", true)
                    .then((res) => {
                        this.presentToast("Directory Created");
                    }, (err) => {
                        console.log("Directory Creation Error:");
                    });
            }
    
            //FILE MOVE CODE
    
            let tempPath = this.imageURI.substr(0, this.imageURI.lastIndexOf('/') + 1);
            let androidPath = this.file.externalRootDirectory + '/DEMO/';
    
            this.file.moveFile(tempPath, this.imageName, androidPath, this.imageName)
                .then((res) => {
                    this.presentToast("Image Moved Successfully");
    
                }, (err) => {
                    this.presentToast("Image Moved Failed");
                });
            //Complete File Move Code
    
            this.toDataURL(this.imageURI, function (dataUrl) {
                console.log('RESULT:' + dataUrl);
            });
        }, (err) => {
            console.log(JSON.stringify(err));
        });
    
    }
     toDataURL(url, callback) {
        let xhr = new XMLHttpRequest();
        xhr.onload = function () {
            let reader = new FileReader();
            reader.onloadend = function () {
                callback(reader.result);
            };
            reader.readAsDataURL(xhr.response);
        };
        xhr.open('GET', url);
        xhr.responseType = 'blob';
        xhr.send();
    }