How to convert a file to Base64?

android, base64

Solution

An updated, more efficient, Kotlin version, that bypasses Bitmaps and doesn't store entire ByteArray's in memory (risking OOM errors).

fun convertImageFileToBase64(imageFile: File): String {
    return ByteArrayOutputStream().use { outputStream ->
        Base64OutputStream(outputStream, Base64.DEFAULT).use { base64FilterStream ->
            imageFile.inputStream().use { inputStream ->
                inputStream.copyTo(base64FilterStream)
            }
        }
        return@use outputStream.toString()
    }
}

Problem

Here the report contain the path(pathname in sdcard in string format) ``` File dir = Environment.getExternalStorageDirectory(); File yourFile = new File(dir, report); String encodeFileToBase64Binary = encodeFileToBase64Binary(yourFile); private static String encodeFileToBase64Binary(File fileName) throws IOException { byte[] bytes = loadFile(fileName); byte[] encoded = Base64.encodeBase64(bytes); String encodedString = new String(encoded); return encodedString; } ``` in the byte[] encoded line getting this error. The method encodeBase64(byte[]) is undefined for the type Base64

Original source

Related problems