Thursday, 2 August 2018

Ionic 3 Read URL as base64 in Android

I'm using the Ionic 3 Camera and File plugins to work with image selection from the user's Android gallery app. The plugin returns a file uri which I want to use to extract a base64 encoded image with the File plugin, but I get a NOT_FOUND_ERR when I attempt to read the file from the cache directory. Here's a sample of my code and some log outputs:

this.camera.getPicture(this.getCameraOptionsForPhotoLibrary()).then((fileUri: string) => {

  const fileNameIndex: number = fileUri.lastIndexOf('/') + 1;

  // file:///storage/emulated/0/Android/data/com.myDomain.myApp/cache/
  const cacheDirectoryFromFileUri = fileUri.substring(0, fileNameIndex );

  //  FB_IMG_1532921240445.jpg?1532982282636 
  const fileName = fileUri.substring(fileNameIndex);

  this.file.readAsDataURL(cacheDirectoryFromFileUri, fileName).then(base64 => {
    alert('base64 cacheDirectoryFromFileUri success: ' + JSON.stringify(base64));
  }, err => {
    alert('base64 cacheDirectoryFromFileUri err: ' + JSON.stringify(err));
  });

Please let me know I can provide any more information to help troubleshoot.



from Ionic 3 Read URL as base64 in Android

No comments:

Post a Comment