javascript - Ionic 3 未在图像 src 中显示相机 FILE_URI 路径

标签 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. 当我从相机捕获图像时,它将返回FILE_URI,就像这个file:///storage/emulated/0/Android/data/com.exchangeconnect.sancial/cache/1536751062113-cropped。 jpg?1536751088363,
  2. 但图像未显示在 <img> 中标签

最佳答案

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

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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e1d110c1a11081f530e120b19171053171110171d53091b1c08171b093e4f504c504f" rel="noreferrer noopener nofollow">[email protected]</a>

3]干净的 Cordova 安卓

ionic cordova clean android

请在此处查找更多详细信息 https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/158

关于javascript - Ionic 3 未在图像 src 中显示相机 FILE_URI 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52419904/

相关文章:

javascript - 使用可观察函数更新模板的元素

javascript - 从 Vuejs 中动态创建的数组中的对象中检索值

javascript - 我如何可靠地获取特定参与者离开(和加入)twilio 房间的时间戳?

html - 如何使用 Angular 更改最外层 <body> 标签的背景颜色

ionic3 - 在 ionic3 中找不到 manifest-merger.jar (com.android.tools.build :manifest-merger:26. 0.0)?

javascript - 如何突出显示单词或字母作为搜索中的用户输入?

angular - 在Angular中按对象属性对元素进行分组

angular - 发生未处理的异常 : Configuration 'production' is not set in the workspace

cordova - 错误运行子进程cordova时发生错误

typescript - Ionic 3 - 在警报 Controller 中设置 id 值