I have a file test.docx containing 1 word "Testing". This file is on my dropbox folder and I open this from within the App using contentResolver.
Code:
val inputStream = context.contentResolver.openInputStream(uri)
val allText = inputStream.bufferedReader().use(BufferedReader::readText)
base64Image = Base64.encodeToString(allText.toByteArray(), Base64.NO_WRAP)
the allText gives back some data but when putting this in textfile and saving it as .docx I cannot view it, it's broken. I think I should add a Charset to the bufferedReader method. Tried a couple but none of them work.
What should be the correct code for this?
DOCX
is not plain text. It is a binary format. Use readBytes()
to read in the bytes:
val bytes = context.contentResolver.openInputStream(uri).readBytes()
Bear in mind that your app will crash with an OutOfMemoryError
for large content. Please reconsider your plan to read in the entire content, let alone convert it to base64.