encode Base64 File - Android File Input Output

Android examples for File Input Output:Base64

Description

encode Base64 File

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

public class Main {

    public static String encodeBase64File(String path) {
        File file = new File(path);
        FileInputStream inputFile = null;
        byte[] buffer = new byte[(int) file.length()];
        try {//  w w w .  j  a  v a 2 s  .  c  o  m
            inputFile = new FileInputStream(file);
            inputFile.read(buffer);
            inputFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Base64.encodeToString(buffer, Base64.DEFAULT);
    }
}

Related Tutorials