相机无法在IONIC 4上运行的FILE_URI路径

dr_*_*mio 8 ionic-framework ionic4

使用Cameara拍照时destinationType: this.camera.DestinationType.FILE_URI,生成的URL无法显示图像.例如,在尝试拍摄这样的照片时:

this.camera.getPicture(options).then((url) => {
        // Load Image
        this.imagePath = url;
    }, (err) => {
        console.log(err);
    });
Run Code Online (Sandbox Code Playgroud)

试图显示它<img [src]="imagePath" >会导致错误(找不到文件).

这里的问题是URL在file:///storage...路径中而不是基于localhost的正确路径.

dr_*_*mio 21

在以前的Ionic版本中,这可以通过使用来解决normalizeURL.这对Ionic 4不起作用(或者至少我不能使它工作).

要解决此问题,您需要使用convertFileSrc():

import {WebView} from '@ionic-native/ionic-webview/ngx';
...
this.camera.getPicture(options).then((url) => {
        // Load Image
        this.imagePath = this.webview.convertFileSrc(url);
    }, (err) => {
        console.log(err);
    });
Run Code Online (Sandbox Code Playgroud)

现在,图像URL将采用适当的http://localhost:8080/_file_/storage...格式并正确加载.

有关更多信息,请参阅WKWebView - Ionic Docs.