-3

Unable to convert a pdf file to base64 in android pie, file path returns is "content://com.android.providers.downloads.documents/document/4402" which is not real path hence unable to access the file.

basavaraj ganagi
  • 61
  • 1
  • 2
  • 6

3 Answers3

7

try this

fun convertToBase64(attachment: File): String {
    return Base64.encodeToString(attachment.readBytes(), Base64.NO_WRAP)
}
Ramkumar.M
  • 681
  • 6
  • 21
4

We can use the following whole class into our project to get the real path, I got the solution to my problem

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

After that, I have used the following code to convert pdf file to encode to base64 string

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

basavaraj ganagi
  • 61
  • 1
  • 2
  • 6
1

Support for file:///path uris was discouraged and later on dropped.

You need to use a ContentResolver to access a content://auth/path uri.

tynn
  • 38,113
  • 8
  • 108
  • 143