Ionic window.resolveLocalFileSystemURL not working for jpgs

I am using gallery to access images for upload. I have no problem when I upload the uri image directly, but when I need to use fileEntry (for multi-file uploads) the jpgs do not return fileEntry from their uri. Note: I am using async: wait, but I have the same problem with standard promises

For example the following code I am using to get the image uri

var imageUri = await this.takePicture(Object.assign(this.defaultOptions,{sourceType:this.camera.PictureSourceType.SAVEDPHOTOALBUM}));
let newFile = new File([imageUri], "filename");
this.uploadFile(newFile);

      

Below is the code for takePicture()

that returns imageUri

try{
  return await this.camera.getPicture(options);
}catch(err){
  ...handle error
}

      

This code above successfully downloads files png

and jpg

.

This code below cannot get file Entry for files only jpg

.

    /**
   * Returns a file entry for the image uri provided
   * @param imgUri image Uri to get file entry for
   */
  private async getFileFromImageUri(imgUri){ 
    try{
      var fileEntry = await this.resolveFileAsPromise(imgUri);
    }catch(err){
      // fileEntry = await this.createNewFileEntry(imgUri);
      this.presentToast('Error while selecting image.');  //TODO: images that are .jpg seem to have trouble being resolved to a file.
    }
    return fileEntry;      
  }

      

resolveFileAsPromise(ImgUri);

is a promise wrapper for window.resolveLocalFileSystemURL(imgUri, resolve, reject);

It returns the following error

FileError {code: 5}

only for jpg. Does the error code mean there were some problems with the jpg format? It doesn't make sense to me since it's all handled internally. The images are not corrupted or something else, so it seems to me that the error code is misleading? error code

Has anyone encountered this problem? I am testing on a real android device (not an emulator).

I feel like the above shows that the image uri is not an issue as it works for loading images when I don't need to access the file. But, when I do this, it only works for certain type of png

s files .

+3


source to share


1 answer


This fixes the problem

var imageUri = await this.camera.getPicture(options);
#FIX
if(this.platform.is('android') && !imageUri.includes("file://")) {
    imageUri = `file://${imageUri}`;
}
var fileEntry = await this.resolveFileAsPromise(imgUri);

      

The problem is that for some reason, on android, the camera library does not return the jpg with full file://

outline, which is required for the File cord plugin.



If all it has to do is preend file://

it makes me wonder why this isn't happening for jpg yet. Moreover, it works correctly / returns file paths for png.

Also, the Cordova returning error code 5 is deceiving as it indicates that there is a problem with the file link and not with this path.

0


source







All Articles