【问题标题】:unable to display image from FILE URI, Ionic framework无法从文件 URI 显示图像,离子框架
【发布时间】:2019-01-13 08:45:34
【问题描述】:

我正在尝试显示从相机拍摄的图像并将其显示到视图中。我在许多网站上搜索过这个答案,但没有任何效果。我尝试过 DomSanitizer、Base64 甚至照片库,但从它们返回的图像没有显示。

我的 home.ts 文件是

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Camera, CameraOptions } from '@ionic-native/camera';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  myPhoto:any;
  image;
  constructor(public navCtrl: NavController, private camera: Camera, public DomSanitizer: DomSanitizer) {

  }

  takePhoto(){
    const options: CameraOptions = {
      quality: 100,
      targetHeight: 320,
      targetWidth: 320,
      destinationType: this.camera.DestinationType.FILE_URI,
      sourceType: this.camera.PictureSourceType.CAMERA,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }

    this.camera.getPicture(options).then((imageData) => {
     // imageData is either a base64 encoded string or a file URI
     // If it's base64 (DATA_URL):
     this.myPhoto = 'data:image/jpeg;base64,' + imageData;
     this.image = imageData;
     console.log(this.myPhoto);
     alert(this.myPhoto);
    }, (err) => {
     // Handle error
    });
  }
}

这是我的 home.html 代码

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <button ion-button (click)="takePhoto()" >Take Photo</button>

  <!-- <img src="{{myPhoto}}"/> -->
  <!-- <img src="{{image}}"/> -->
  <!-- <img [src]="DomSanitizer.bypassSecurityTrustUrl(myPhoto)"/> -->
  <!-- <img data-ng-src="{{myPhoto}}"/> -->
  <img src="data:image/jpeg;base64,file:///storage/sdcard0/Android/data/io.ionic.starter/cache/1533211220154.jpg"/>
</ion-content>

这里的注释代码是我尝试过但没有成功。

【问题讨论】:

标签: ionic-framework ionic2 ionic3 ionic-native


【解决方案1】:

按照以下方式修改你的home.ts

  private openGallery(): void {
    let cameraOptions = {
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      destinationType: this.camera.DestinationType.FILE_URI,
      quality: 100,
      targetWidth: 1000,
      targetHeight: 1000,
      encodingType: this.camera.EncodingType.JPEG,
      correctOrientation: true
    }

    this.camera.getPicture(cameraOptions)
      .then(file_uri => this.imageSrc = file_uri,
      err => console.log(err));
  }

在您的 html 文件中,

<img [src]="imageSrc"/> 

【讨论】:

  • 感谢您的回答,但我通过在 android 8.1.0 上安装 APK 尝试了您的解决方案,但没有成功 :(
  • 可能是权限相关问题。能否请您检查设置中的应用权限。
  • 已检查存储权限。
【解决方案2】:

在你的 ts 文件中使用这个函数

takePhoto(){
    const options: CameraOptions = {
      quality: 100,
      targetHeight: 320,
      targetWidth: 320,
      destinationType: this.camera.DestinationType.DATA_URL,
      sourceType: this.camera.PictureSourceType.CAMERA,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }

    this.camera.getPicture(options).then((imageData) => {
      // imageData is either a base64 encoded string or a file URI
      // If it's base64 (DATA_URL):
      this.myPhoto = 'data:image/jpeg;base64,' + imageData;
      this.image = imageData;
      console.log(this.myPhoto);
    }, (err) => {
      // Handle error
    });
  }

在你的 html 中使用它

<img [src]="myPhoto"/>

【讨论】:

  • 感谢您的回答,但我通过在 android 8.1.0 上安装 APK 尝试了您的解决方案,但没有成功 :(
  • 我没有出错,如果我控制台 this.myPhoto 我得到文件路径,但我看到的图像被破坏了。
  • 如果你设置destinationType: this.camera.DestinationType.DATA_URL你应该得到一个base64编码的字符串。
  • 我尝试通过以下方式在视图中显示图像: * 使用和不使用 Base64 字符串 * 使用 data_url 和 file_url * 检查存储权限 我正在获取文件的路径,我在存储上检查了它但仍然无法在视图中显示它。仅供参考 我的 ionic 版本是 4.0.5 NPM 版本: 6.1.0 node -v 10.7.0 cordova -v 8.0.0
  • DATA_URL 应该避免,因为(它可能会占用大量内存并导致应用程序崩溃或内存不足错误。如果可能,请使用 FILE_URI 或 NATIVE_URI)对于 FILE_URI,我们可以将其转换为 base64 读取(如果需要)
【解决方案3】:

我使用了文件插件及其方法 readAsDataURL。它对我来说很好。希望它会帮助那里的人:)

const options: CameraOptions = {
      quality: 100,
      allowEdit: true,
      sourceType:  this.camera.PictureSourceType.CAMERA ,
      saveToPhotoAlbum: true,
      correctOrientation: true,
      encodingType: this.camera.EncodingType.JPEG,
      destinationType: this.camera.DestinationType.FILE_URI
    }

this.camera.getPicture(options).then((imageData) => {

    //imageData will hold the full file path so we need to extract filename and path using file plugin 
     var filename = imageData.substring(imageData.lastIndexOf('/')+1);
     var path =  imageData.substring(0,imageData.lastIndexOf('/')+1);
    //then use it in the readAsDataURL method of cordova file plugin
    //this.file is declared in constructor file :File
         this.file.readAsDataURL(path, filename).then(res=>{
           item.pic = res;
         });
}, (err) => {
 alert(err);
});

link for ionic cordova file plugin

【讨论】:

  • 从图库中获取图像时无法正常工作,仅在 android 中,在 iOS 中可以正常工作,有人有解决方案吗?
  • readAsDataURL 方法将图像文件 url 转换为 base64 img。那么为什么不使用插件中的直接base64字符串(destinationType:this.camera.DestinationType.DATA_URI)
【解决方案4】:

终于找到解决办法了:

在组件中:

takePhoto(){
    const options: CameraOptions = {
      quality: 100,
      targetHeight: 320,
      targetWidth: 320,
      destinationType: this.camera.DestinationType.FILE_URI,
      sourceType: this.camera.PictureSourceType.CAMERA,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }

    this.camera.getPicture(options).then((imgFileUri) => {
     this.myImg = (<any>window).Ionic.WebView.convertFileSrc(imgFileUri);
    }, (err) => {
     console.log(err);
    });
  }

在 html 中:

<img [src]="myImg" />

注意: cordova-plugin-ionic-webview >= 4

【讨论】:

  • 我运气不好 :(。不知道为什么这不起作用。我现在花了 2 天时间
  • 你试过 ionic 3 吗?
猜你喜欢
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 2016-06-12
  • 2019-12-15
  • 1970-01-01
  • 2014-06-04
  • 2017-12-05
  • 2014-05-18
相关资源
最近更新 更多