Saving .jpg using @ionic-native/file seems to corrupt it somehow


#1

No errors while running, but when viewing the resulting base64 in page the image is corrupt. The base64 string when reading the image after saving it is a lot longer than the one im asking it to save.

Related code:

this.pictureDir=this.file.dataDirectory+'pictures/';


     public saveImage(base64:string, name:string):Promise<string>{
    return new Promise((resolve, reject)=>{
      this.file.writeFile(this.pictureDir, name, base64, )
      .then((data)=>{
        resolve(this.pictureDir+name);
      })
      .catch((err)=>{
        console.log(err.message)
        reject(err);
      })
    })
  }
  public imgUriToBase64(imgUri:string):Promise<string>{
    return new Promise((resolve, reject)=>{
      let path:[string,string]=CatchService.splitUri(imgUri);
      this.file.readAsDataURL(path[0],path[1])
      .then((data)=>{
        resolve(data);
      })
      .catch((err)=>{
        console.log(err.message);
        reject(err);
      })
    })
  }


public static splitUri(imgUri:string):[string,string]{
    let path:string[]=new Array<string>();
    let file:string[]=new Array<string>();
    let fileFound:boolean=false;
    for(let i:number=imgUri.length-1;i>=0;i--){
      if(imgUri.charAt(i)=='/'){
        fileFound=true;
      }
      if(!fileFound){
        file.push(imgUri.charAt(i));
      }else{
        path.push(imgUri.charAt(i));
      }
    }
    let returnPath:string='';
    let returnFile:string='';
    for(let c of path){
      returnPath=c+returnPath;
    }
    for(let c of file){
      returnFile=c+returnFile;
    }
    return [returnPath, returnFile];
  }

Thanks for the help.
Sorry if wrong format etc, new to the forum


#2

This is how far i have come:
The image is saved with the native camera app, then read in base64 with .readAsDataURL from File. This base64 i can show in my page with no problems.
Then i save the file to a picture folder with .writeFile. When using the resulting uri to show the file in my page, its corrupted.

Does .writeFile save a base64 encoded image correctly?


#3

my base64 saving is bad, and I should feel bad.

my saveImage function is not how you save a base64 image at all, at least not withouth removing the “data:image/jpeg:base64,” prefix and maybe converting back to binary first, which im not going into right now.

solved my copying issues with @ionic-native/file’s .copyFile function

Still getting some weird errors when sending the base64 encoded image with json, but it might be a server related issue