Mohan Gopi
Mohan Gopi

Reputation: 7724

Ionic3: Base64 To Gallery Plugin crashes the app

I am trying to save the base64 string to the gallery. When I invoke this plugin my code get's crashed. Here is the link I used to check.

code I use

let options:Base64ToGalleryOptions = { prefix: '_img',mediaScanner: true }

   //after the below line my gets close automatically any idea
      this.base64ToGallery.base64ToGallery(base64Image[1],options)
      .then(
        res => {
          debugger
          console.log('Saved image to gallery ', res)
        },
        err => {
          debugger
          console.log('Error saving image to gallery ', err)
        });

I am not able to debug

I am not able to understand why my app closes automatically after hitting this code

Update:

After installing this particular version of the plugin

 ionic cordova plugin add [email protected]

and moving my code to platform

this.platform.ready().then(() => {

      this.base64ToGallery.base64ToGallery(base64Image,options)
      .then(
        res => {
          console.log('Saved image to gallery ', res);
          this.navCtrl.pop();
        },
        err => { //For ios i am getting as `plugin_not_installed`
          console.log('Error saving image to gallery ', err);
          this.navCtrl.pop()
        });

    })

But this same code is not working for ios according to the doc i have installed the same version which supports ios also (2.0.2) but it looks something is missing if any please let me know

Upvotes: 13

Views: 2537

Answers (1)

Garrett Barlocker
Garrett Barlocker

Reputation: 703

Since you are unable to debug here are three problems I ran across until I got it to work, most likely the second problem if on Android or the third problem if on iOS.

1) Error saving image to gallery cordova_not_available

Fix for this was to create a project that had cordova baked in with the command ionic start blank --cordova

2) Error saving image to gallary Error while saving image I got this error message on an Android device. I looked at their code implementation here https://github.com/Nexxa/cordova-base64-to-gallery/blob/2f531aaa0bf17b900cf6bd9704082e72f183d325/src/android/Base64ToGallery.java

Saw that they have not done anything regarding WRITE_EXTERNAL_STORAGE permissions.

My solution was to add AndroidPermissions and check for WRITE_EXTERNAL_STORAGE permissions at runtime.

hasWriteAccess: boolean = false;
constructor(private base64ToGallery: Base64ToGallery,
   private androidPermissions: AndroidPermissions) {
}

ionViewWillEnter() {
   this.checkPermissions();
}

checkPermissions() {
   this.androidPermissions
   .checkPermission(this.androidPermissions
   .PERMISSION.WRITE_EXTERNAL_STORAGE)
   .then((result) => {
    console.log('Has permission?',result.hasPermission);
    this.hasWriteAccess = result.hasPermission;
  },(err) => {
      this.androidPermissions
        .requestPermission(this.androidPermissions
        .PERMISSION.WRITE_EXTERNAL_STORAGE);
   });
   if (!this.hasWriteAccess) {
     this.androidPermissions
       .requestPermissions([this.androidPermissions
       .PERMISSION.WRITE_EXTERNAL_STORAGE]);
   }
}

saveImage() {
   if (!this.hasWriteAccess) {
     this.checkPermissions();
   }
   let options: Base64ToGalleryOptions = {
     prefix: '_img', 
     mediaScanner: true
   };
   this.base64ToGallery
     .base64ToGallery(this.base64Data, options).then(
     res => console.log('Saved image to gallery:', res),
     err => console.log('Error saving image to gallery:', err)
   );
}

3) This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.

Solution is to add NSPhotoLibraryAddUsageDescription to project_name/config.xml nested between <platform name="ios"> and </platform>

<config-file parent="NSPhotoLibraryAddUsageDescription" target="*-Info.plist">
    <string>Saves images from base64 to your Photo Library</string>
</config-file>

Upvotes: 2

Related Questions