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
.
Asked
Active
Viewed 1.0k times
-3

basavaraj ganagi
- 61
- 1
- 2
- 6
-
What's your question? – Zun Feb 25 '19 at 09:56
-
I want to encode a pdf file to base64 string – basavaraj ganagi Feb 25 '19 at 10:08
-
No, that's not your question. That's your goal. Your question is how to get the correct file path for a pdf file – Zun Feb 25 '19 at 10:27
-
Right please help me on that – basavaraj ganagi Feb 25 '19 at 10:32
-
I can't without seeing your code – Zun Feb 25 '19 at 10:35
3 Answers
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
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