1

I'm using the following code to get to access the device Photo Library, however it is retuning base64 encoded string, so I am a bit confused on what to do with this information. I would like to store the photo to the application storage directory (not the device photo album) then get the image URL so I can add it to the user DB.

takePhoto() {
console.log('take photo')
const options: CameraOptions = {
  quality: 100,
  destinationType: this.camera.DestinationType.DATA_URL,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE,
  sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
}
this.camera.getPicture(options).then((imageData) => {
  // imageData is either a base64 encoded string or a file URI
  // If it's base64:
  console.log(imageData)
  let base64Image = 'data:image/jpeg;base64,' + imageData;

}, (err) => {
  // Handle error
  console.log('camera error')
  console.log(err)
});

}

Sampath
  • 63,341
  • 64
  • 307
  • 441
colouredFunk
  • 778
  • 1
  • 13
  • 35

1 Answers1

0

Try to use

destinationType : Camera.DestinationType.FILE_URI

This plugin implements a File API allowing read/write access to files residing on the device. cordova-plugin-file

Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
  • How would this work if they take a photo with the camera? – colouredFunk Oct 15 '17 at 09:37
  • Where would like to store the photo? You can't store to the application storage directory then get the image URL.Because URL is reference to a web resource that specifies its location on a computer network. You should send base64 or jpeg file to your server and your server code will store it and will save url to your database. Right? – Dmitry Grinko Oct 15 '17 at 10:04
  • I didn't know that. The user has the options to add a photo via the camera or their Library, ..and eventually Facebook (phase 2), so ideally it would be consistent across all methods. In past, I would save the image to the device then just store the path in the DB, but you are right it would be better to store it on the server. How would this work between base64 and a jpg path? – colouredFunk Oct 15 '17 at 10:54
  • If you use nodejs on your server you can transform base64 to file and save your file like in this example https://stackoverflow.com/a/20272545/6108211 If you use php - https://stackoverflow.com/a/15153931/6108211 – Dmitry Grinko Oct 15 '17 at 13:00