Encode file content to Base64 string From Path - Android java.io

Android examples for java.io:File

Description

Encode file content to Base64 string From Path

Demo Code

import android.util.Base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Main{

    public static String getBase64FromPath(String path) {
        String base64 = "";
        try {/*from   w w w .  ja  va  2s  .  c om*/
            File file = new File(path);
            byte[] buffer = new byte[(int) file.length() + 100];
            @SuppressWarnings("resource")
            int length = new FileInputStream(file).read(buffer);
            base64 = Base64.encodeToString(buffer, 0, length,
                    Base64.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return base64;
    }

}

Related Tutorials