拍照并将其保存在为我的应用程序创建的特定文件夹中

时间:2019-08-13 07:30:13

标签: angular cordova ionic4 ionic-native

我开发了一个带有ionic 4和angular的移动应用程序,我想拍摄一张照片并将其保存到我同时创建的文件夹中,并恢复路径。 使用ionic框架,当我尝试阅读de var imageData时,我有了:

  

“ content://com.android.providers.media.documents/document/image%3A39670”,而不是基本路径,

所以我不明白如何在应用程序中恢复图像并将其显示在应用程序中

currentImage: any;
public takePhoto() {
    console.log('Camera Photo Click');
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: true,
    }
    this.camera.getPicture(options).then((imageData) => {
    let base64Image = 'data:image/jpeg;base64,' + imageData;

    this.file.writeFile(base64Image, 'interv-app-pics', 'Sample.jpg' );

    this.currentImage = imageData;

    }, (err) => {
    // Handle error
    });
  }
  public writeFile(base64Data: any, folderName: string, fileName: any) {
    let contentType = this.getContentType(base64Data);
    let DataBlob = this.base64toBlob(content, contentType);
    // here iam mentioned this line this.file.externalRootDirectory is a native pre-defined file path storage.
    // You can change a file path whatever pre-defined method.
    let filePath = this.file.externalRootDirectory + folderName;
    this.file.writeFile(filePath, fileName, DataBlob, contentType)
      .then((success) => {
        console.log('File Writed Successfully', success);
    }).catch((err) => {
        console.log('Error Occured While Writing File', err);
    })
  }

  public base64toBlob(b64Data, contentType) {
    var sliceSize;
    contentType = contentType || '';
    sliceSize = 512;
    let byteCharacters = atob(b64Data);
    let byteArrays = [];
    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        let slice = byteCharacters.slice(offset, offset + sliceSize);
        let byteNumbers = new Array(slice.length);
        for (let i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }
        var byteArray = new Uint8Array(byteNumbers);
        byteArrays.push(byteArray);
    }
    let blob = new Blob(byteArrays, {
        type: contentType
    });
    return blob;
  }
  public getContentType(base64Data: any) {
    let block = base64Data.split(';');
    let contentType = block[0].split(':')[1];
    return contentType;
}

0 个答案:

没有答案