显示图库图像

时间:2019-01-31 05:13:33

标签: ionic-framework ionic3 cordova-plugins hybrid-mobile-app ionic-native

我有一个应用程序,允许用户从图库中选择照片或通过相机拍摄照片。我正在尝试显示通过相机拍摄的图像,并且效果很好,它返回文件路径,如

file:///Users/ray/Library/Developer/CoreSimulator/Devices/C8202B3B-300B-4819-8CD3-4C4AA690CE7C/ data/Applications/D82BF64E-6DD1-4645-B637-BCF65001FD29/tmp/cdv_photo_003.jpg

但是当我尝试从图库中选择照片时,它会显示损坏的图像缩略图,并且它会像这样更改文件路径。

content://com.android.providers.media.documents/document/image%3A21

离子CLI版本: PRO 4.2.1

科尔多瓦版本: 8.0.0

NPM版本: 6.4.1

Node.js版本: 8.11.3

平台: Android

我也尝试过寻找解决方案,但没有成功,或者我仍然做错了事

其中有些人建议使用此代码

if (imageURI.substring(0,21)=="content://com.android") {
  photo_split=imageURI.split("%3A");
  imageURI="content://media/external/images/media/"+photo_split[1];
}

,但是此解决方案不是很可靠,因为并非所有图像源都返回包含'content://com.android'的相同文件路径,就像来自 Google相册会返回“ content://com.google.android”

_其中一些人还建议在目标类型上使用 DATA_URL ,但这会占用大量内存,并可能导致应用崩溃_

这是我的代码:

TS文件

selectImage(sourceType) {
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: true,
      sourceType: sourceType
    }

    this.camera.getPicture(options).then((imageData) => {
     let base64Image = 'data:image/jpeg;base64,' + imageData;
     this.imagePreview = imageData;
    }, (err) => {
      this.toastCtrl.presentToast(err);
    });
  }

HTML文件

<img src="{{imagePreview}}" />;

我希望有人可以帮助我。预先谢谢你。


请帮助!!!请...

4 个答案:

答案 0 :(得分:0)

ts:

public base64Image:string; 
public getImage(){
const options: CameraOptions = {
  quality: 100,
  destinationType: this.camera.DestinationType.DATA_URL,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE
}

this.camera.getPicture(options).then((imageData) => {
 this.base64Image = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
 // Handle error
});
}

html:

<imge src={{base64Image}} />

答案 1 :(得分:0)

尝试一下:-

   //From Gallery
     ChooseGallery() {
        var _self=this;
        const options: CameraOptions = {
          allowEdit: true,
          correctOrientation: true,
          quality: 100, // picture quality
          destinationType: this.camera.DestinationType.FILE_URI,
          encodingType: this.camera.EncodingType.JPEG,
          sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM
        }
        this.camera.getPicture(options).then((ImageData) => {
          _self.base64value = ImageData;
        })
      }

从相机

  ChooseCamera() {
    var _self=this;
    const options: CameraOptions = {
      allowEdit: true,
      correctOrientation: true,
      quality: 100, // picture quality
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }
    this.camera.getPicture(options).then((ImageData) => {
      _self.base64value =  ImageData;
    })
  }

我希望这会有所帮助!谢谢!

答案 2 :(得分:0)

答案 3 :(得分:0)

let options: CameraOptions = {
  quality: 100,
  sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM,
  saveToPhotoAlbum: true,
  correctOrientation: true,
  encodingType: this.camera.EncodingType.JPEG,
  destinationType: this.camera.DestinationType.FILE_URI
};

this.camera.getPicture(options).then((imageData) => {
  let photo_split=imageData.split("%3A");
  let photo_split2=photo_split[0].split("?");
  let filename = photo_split2[0].substring(photo_split2[0].lastIndexOf('/')+1);
  let path =  photo_split2[0].substring(0,photo_split2[0].lastIndexOf('/')+1);

  alert(photo_split2[0])
  alert(filename)
  alert(path)
  this.file.readAsDataURL(path, filename).then(res=> {
    alert("FUNCIONA!")
    this.imageURI = res;
  }).catch((reason) => {
    alert(JSON.stringify(reason));
  });
}).catch((reason) => {
  alert(JSON.stringify(reason));
});

// HTML

    <img alt="uploaded Image" [src]="imageURI">