简体   繁体   English

Base64 插件不起作用

[英]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.我想捕捉的图像从相机发送base64字符串到服务器,这很简单,因为我可以直接拿到base64所拍摄的图像,但当时我使用的本地农作物,它返回的URI裁剪图像。 So now I HAVE to get the base64 of this image, but the Base64 plugin isn't working anymore.所以现在我必须得到这个图像的base64 ,但是Base64插件不再工作了。 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.我对代码进行了一些更改,并用 File Plugin 替换了 Base64 插件。

  1. Add File plugin to app.module.ts imports.将文件插件添加到 app.module.ts 导入。
  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.在 Android 中,在 build.gradle(app) 文件中,将目标 SDK 更改为 targetSdkVersion 30。

Then go to your AndroidManifest.xml and add this line android:requestLegacyExternalStorage="true" in the application tag like that :然后转到您的 AndroidManifest.xml 并在应用程序标记中添加这一行 android:requestLegacyExternalStorage="true" ,如下所示:

<?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 Base64插件需要安装设备

See the repo phonegap-base64 line 11请参阅 repo phonegap-base64 line 11

if (device.platform == "Android")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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