Ionic native Base64 To Gallery doesn't work


#1

Hello,

I’m trying to use the “Base64 To Gallery” plugin from Ionic native but it doesn’t work.

Base64ToGallery.base64ToGallery(base64Data, ‘img_’).then(
res => console.log("Saved image to gallery ", res),
err => console.log("Error saving image to gallery ", err)
);

I’m working on Ionic 2 TS and have this error :

Error TS2339: Property ‘base64ToGallery’ does not exist on type ‘typeof Base64ToGallery’

If someone already figure out this problem it will be nice to get some advices.

Best regards,
François


#2

maybe there are missing typings for Base64ToGallery. Try this:

Base64ToGallery['base64ToGallery'](base64Data, 'img_').then(
  res => console.log("Saved image to gallery ", res),
  err => console.log("Error saving image to gallery ", err)
);

#3

Thanks for the quick reply I really appreciate.

Unfortunately, there is another error :

EXCEPTION: TypeError: ionic_native_1.Base64ToGallery[‘base64ToGallery’] is not a function. (In ‘ionic_native_1.Base64ToGallery’base64ToGallery’’, ‘ionic_native_1.Base64ToGallery[‘base64ToGallery’]’ is undefined)


#4

And you added the cordova plugin correctly to your app?
https://www.npmjs.com/package/cordova-base64-to-gallery


#5

Yes I did.

I changed the following files like this and now it’s work.

base64togallery.js

“use strict”;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === “object” && typeof Reflect.decorate === “function”) r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i–) if (d = decorators[i]) r = (c < 3 ? d® : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var plugin_1 = require(’./plugin’);
/**

  • @name Base64 To Gallery
  • @description This plugin allows you to save base64 data as a png image into the device
  • @usage
  • import {Base64ToGallery} from ‘ionic-native’;
  • Base64ToGallery.base64ToGallery(base64Data, ‘img_’).then(
  • res => console.log("Saved image to gallery ", res),
  • err => console.log("Error saving image to gallery ", err)
  • );

/
var Base64ToGallery = (function () {
function Base64ToGallery() {
}
/
*
* Converts a base64 string to an image file in the device gallery
* @param data
* @param prefix
*/
Base64ToGallery.base64ToGallery = function (data, prefix, mediaScanner) { return; };
__decorate([
plugin_1.Cordova()
], Base64ToGallery, “base64ToGallery”, null);
Base64ToGallery = __decorate([
plugin_1.Plugin({
plugin: ‘cordova-base64-to-gallery’,
pluginRef: ‘cordova’,
repo: ‘https://github.com/Nexxa/cordova-base64-to-gallery’,
platforms: [‘Android’, ‘iOS’, ‘Windows Phone 8’]
})
], Base64ToGallery);
return Base64ToGallery;
}());
exports.Base64ToGallery = Base64ToGallery;
//# sourceMappingURL=base64togallery.js.map

and

base64togallery.d.ts

/**

  • @name Base64 To Gallery
  • @description This plugin allows you to save base64 data as a png image into the device
  • @usage
  • import {Base64ToGallery} from ‘ionic-native’;
  • Base64ToGallery.base64ToGallery(base64Data, ‘img_’).then(
  • res => console.log("Saved image to gallery ", res),
  • err => console.log("Error saving image to gallery ", err)
  • );

/
export declare class Base64ToGallery {
/
*
* Converts a base64 string to an image file in the device gallery
* @param data
* @param prefix
*/
static base64ToGallery(data: string, prefix?: string, mediaScanner?: boolean): Promise;
}

Thanks for your help.

Best regards,
François


#6

If this is a general issue with the plugin --> create a pull request for the plugin, because hacking in plugin code is not the best solution ;).


#7

This still doesn’t work without the modifications above. Has the change above been merged?


#8

Hi, I am having also problem with above plugin.

I’ve installed plugin as described on here

Error i am getting is: “data.replace” is not a function.

It seems i don’t have content of base64togallery.js as above. But i found this
data.replace
within the code inside
plugins/cordova-base64-to-gallery/src/www/base64togallery.js
as well as
node_modules/cordova-base64-to-gallery/src/www/base64togallery.js
I’ve tried to change data.replace to data.result.split(',')[1] in both fies.
Than i ran:
ionic cordova run android
Unfortunatelly i am getting same error, not sure why it did not include my changes.
Any help much appreciated.

Thanks


#9

Ok so in my case it was simply need of adding permissions to the page constructor which was trying to save image into gallery. Code below solved it:

this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE).then(
      result => console.log("Permissions granted", result.hasPermissions),
      error => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
    );
    this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE]);