当使用Cameara与destinationType: this.camera.DestinationType.FILE_URI拍照时,生成的URL将无法显示图像。例如,当尝试拍摄这样的照片时:
this.camera.getPicture(options).then((url) => {
// Load Image
this.imagePath = url;
}, (err) => {
console.log(err);
});尝试将其显示为<img [src]="imagePath" >将导致错误(找不到文件)。
这里的问题是URL在file:///storage...路径中,而不是基于本地主机的正确路径。
发布于 2018-09-03 05:14:06
在以前的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);
});现在,图像URL将采用适当的http://localhost:8080/_file_/storage...格式,并将正确加载。
有关详细信息,请参阅WKWebView - Ionic Docs。
发布于 2019-10-20 01:07:07
在我的例子中,下面的代码适用于我
const downloadFileURL = 'file:///...';
// Convert a `file://` URL to a URL that is compatible with the local web server in the Web View plugin.
const displayedImg = (<any>window).Ionic.WebView.convertFileSrc(downloadFileURL);发布于 2019-11-20 07:35:05
如果这里有一些人在ionic4上寻找答案,请查看以下内容
"Not allowed to load local resource" for Local image from Remote page in PhoneGap Build
寻找@Alok Singh的答案,这就是我如何让它在ionic4上工作,甚至在livereload上工作
https://stackoverflow.com/questions/52141085
复制相似问题