简体   繁体   中英

Base64 plugin not working

The plugin was working fine until recently I started facing problems with it, the code does not execute neither does it throw an error. I want to capture images from camera and send the base64 string to the server, which was easy since I could directly get the base64 of the captured image but then I used the native crop which returns the URI of the cropped image. So now I HAVE to get the base64 of this image, but the Base64 plugin isn't working anymore. Any workaround or help is really appreciated. Code I used:

this.base64.encodeFile(filePath).then((base64File: string) => {
    console.log(base64File);    // Won't execute
}, (err) => {
    console.log(err);  // Won't execute
});

I spent couple of hours on the same bug. It occurred that this plugin just stopped working (it's still beta version). I made some changes in my code and replaced Base64 plugin with File Plugin.

  1. Add File plugin to app.module.ts imports.
  2. Import and inject dependency in component/service constructor.

And then if you have filepath:

// split file path to directory and file name
let fileName = filePath.split('/').pop();
let path = filePath.substring(0, filePath.lastIndexOf("/") + 1);

this.file.readAsDataURL(path, fileName)
.then(base64File => {
    console.log("here is encoded image ", base64File)
})
.catch(() => {
    console.log('Error reading file');
})

In Android, in the build.gradle(app) file, change target SDK to targetSdkVersion 30.

Then go to your AndroidManifest.xml and add this line android:requestLegacyExternalStorage="true" in the application tag like that :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    >

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:requestLegacyExternalStorage="true">

try to apply this solution is working for me

Base64 plugin is need install Device

See the repo phonegap-base64 line 11

if (device.platform == "Android")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM