离子3在图像src中未显示相机FILE_URI路径

时间:2018-09-20 07:33:53

标签: javascript angular ionic3 angular5

这是我的HTML代码

<img [src]="profileImage"/>
<button (click)="openCamera()"> Upload </button>

下面是我的组件代码

import { ViewController, Platform, normalizeURL } from 'ionic-angular';

openCamera() {
let options = {
    quality: 100,
    destinationType: this.camera.DestinationType.FILE_URI,
    encodingType: this.camera.EncodingType.JPEG,
    mediaType: this.camera.MediaType.PICTURE,
    saveToPhotoAlbum: false,
    targetWidth: 400,
    targetHeight: 400,
    allowEdit: false
}

 this.camera.getPicture(options).then((imageData) => {
    this.cropService.crop(imageData, { quality: 100 }).then((newImage) => {
        this.profileImage = normalizeURL(newImage);
    }, (error) => {
        console.log("Error cropping image", error);
    });
}, (err) => {
    console.log('Error camera image', err);
});
}
  1. 当我从Camera捕获图像时,它会返回FILE_URI,就像 file:///storage/emulated/0/Android/data/com.exchangeconnect.sancial/cache/1536751062113-cropped一样。 jpg?1536751088363
  2. 但图片未显示在<img>标签中

2 个答案:

答案 0 :(得分:2)

我已经修改了您的代码。请在下面找到更新的代码:

import { ViewController, Platform, normalizeURL } from 'ionic-angular';

openCamera() {
    let options = {
        quality: 100,
        destinationType: this.camera.DestinationType.FILE_URI,
        encodingType: this.camera.EncodingType.JPEG,
        mediaType: this.camera.MediaType.PICTURE,
        saveToPhotoAlbum: false,
        targetWidth: 400,
        targetHeight: 400,
        allowEdit: false
    }

     this.camera.getPicture(options).then((imageData) => {
        this.cropService.crop(imageData, { quality: 100 }).then((newImage) => {
            if (this.platform.is('ios')){
                this.profileImage = normalizeURL(newImage);
            }else{
                this.profileImage= "data:image/jpeg;base64," + newImage;
            }
        }, (error) => {
            console.log("Error cropping image", error);
        });
    }, (err) => {
        console.log('Error camera image', err);
    });
}


如果上述解决方案不起作用,请尝试以下代码:

    import { ViewController, Platform, normalizeURL } from 'ionic-angular';

    openCamera() {
      let options = {
        quality: 100,
        destinationType: this.camera.DestinationType.FILE_URI,
        encodingType: this.camera.EncodingType.JPEG,
        mediaType: this.camera.MediaType.PICTURE,
        saveToPhotoAlbum: false,
        targetWidth: 400,
        targetHeight: 400,
        allowEdit: false
    }

    this.camera.getPicture(options).then((imageData) => {
          // don't need to crop image for iOS platform as it is a in-build feature
          if (this.platform.is('ios')) {
            this.getFileUri(imageData);
          }else { // android platform, we need to do manually
            this.cropService.crop(imageData, { quality: 100 }).then(newImage => {
                this.getFileUri(newImage);
              },
              error => console.error('Error cropping image', error)
            );
         }
    }, (err) => {
        console.log('Error camera image', err);
    });
}

private getFileUri = (url: any) => {
    var scope = this;
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
      var reader = new FileReader();
      reader.onloadend = function() {
        scope.profileImage = reader.result;
      }
      reader.readAsDataURL(xhr.response);
    };
    xhr.open('GET', url);
    xhr.responseType = 'blob';
    xhr.send();
}

我认为它与“ cordova-plugin-ionic-webview”插件有关。请参考以下步骤解决该问题:
1]卸载现有的Webview插件

ionic cordova plugin rm cordova-plugin-ionic-webview

2]安装旧版本

ionic cordova plugin add cordova-plugin-ionic-webview@1.2.1

3]干净的cordova android

ionic cordova clean android

请在这里https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/158

中找到更多详细信息

答案 1 :(得分:0)

尝试保留原始代码并进行更改

saveToPhotoAlbum: false

saveToPhotoAlbum: true