androidcordovaionic-frameworkcordova-plugin-file

Cordova-plugin-file Encoding_Err on Android


I have an Ionic 3 App where I can upload picture from a device. This feature works well with IOS, but fail on Android with the error code 5 (Encoding error).

This is the flow :

protected takePicture(source: number, callback: any) {
  const options: CameraOptions = {
    sourceType: source,
    mediaType: 2,
  };
  this.camera.getPicture(options).then((imageData) => {
    return callback(imageData)
  });
}

And then :

let promise = new Promise((resolve, reject) => {
    let prefix = '';
    if (i.indexOf('file://') === -1 && i !== '') {
      prefix = 'file://';
    }
    this.file.resolveLocalFilesystemUrl(prefix + i).then((fileEntry: any) => {
      fileEntry.file((file: any) => {
        let reader = new FileReader();
        if (file.size > 99999999) {
          return this.events.publish('error', this.errorUploadMessage, false);
        }
        reader.onloadend = function(e) {
          let blob = new Blob([this.result], { type: file.type });
          let filename = file.name;
          let extension = filename.match(/^.*\./);
          if (!extension || extension === '') {
            let type = file.type.split('/').pop();
            filename = filename + '.' + type;
          }
          resolve({ blob: blob, name: filename });
        };
        reader.readAsArrayBuffer(file);
      });
    }, (error) => {
      console.log(error);
      this.events.publish('error', this.errorUploadMessage, false);
    });
  });

Everything work well with IOS so I don't understand why not with android. When I check the path here: this.file.resolveLocalFilesystemUrl(prefix + i)

I have this : file://content://com.android.providers.media.documents/document/image%3A1313

Maybe the problem comes from 'image%3A1313' at the end. On IOS I can se the real picture name and extension (.jpeg for exemple).

I already checked several issues on SOF, but nothing work or seems to be revelant to my issue.


Solution

  • Ok just found the solution... removing the prefix ("file://") seems to do the trick... It's strange because I never seen this suggestion anywhere..