basheer basheer - 8 months ago 58
Java Question

How to convert a file to base 64 (like .pdf,.txt) in Android?

How to covert the SD-card documents (.pdf,.txt) to base 64 string and string send to the server

Answer Source

Try these codes

        File dir = Environment.getExternalStorageDirectory();
        File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
        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;