簡體   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