繁体   English   中英

Base64 插件不起作用

[英]Base64 plugin not working

该插件运行良好,直到最近我开始遇到它的问题,代码既不执行也不抛出错误。 我想捕捉的图像从相机发送base64字符串到服务器,这很简单,因为我可以直接拿到base64所拍摄的图像,但当时我使用的本地农作物,它返回的URI裁剪图像。 所以现在我必须得到这个图像的base64 ,但是Base64插件不再工作了。 任何解决方法或帮助都非常感谢。 我使用的代码:

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

我在同一个错误上花了几个小时。 碰巧这个插件刚刚停止工作(它仍然是测试版)。 我对代码进行了一些更改,并用 File Plugin 替换了 Base64 插件。

  1. 将文件插件添加到 app.module.ts 导入。
  2. 在组件/服务构造函数中导入和注入依赖项。

然后如果你有文件路径:

// 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');
})

在 Android 中,在 build.gradle(app) 文件中,将目标 SDK 更改为 targetSdkVersion 30。

然后转到您的 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">

尝试应用此解决方案对我有用

Base64插件需要安装设备

请参阅 repo phonegap-base64 line 11

if (device.platform == "Android")

暂无
暂无

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

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