如何在kotlin android中将pdf文件编码为base64字符串

问题描述 投票:-4回答:3

无法将pdf文件转换为android pie中的base64,文件路径返回的是“content://com.android.providers.downloads.documents/document/4402”,这不是真正的路径因此unable to access the file

android kotlin kotlin-android-extensions
3个回答
1
投票

不鼓励支持file:///path uris,后来又放弃了。

您需要使用ContentResolver访问content://auth/path uri。


1
投票

试试这个

fun convertToBase64(attachment: File): String {
    return Base64.encodeToString(attachment.readBytes(), Base64.NO_WRAP)
}

1
投票

我们可以将以下整个类用于我们的项目以获得真正的路径,我得到了解决我的问题的方法

https://github.com/flutter/plugins/blob/master/packages/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/FileUtils.java

之后,我使用以下代码将pdf文件转换为编码为base64字符串

fun convertToBase64(attachment: File): String { return Base64.encodeToString(attachment.readBytes(), Base64.NO_WRAP) }

© www.soinside.com 2019 - 2024. All rights reserved.