Android Base64 File Encode encodeFile(String filePath)

Here you can find the source of encodeFile(String filePath)

Description

encode File

Declaration

public static String encodeFile(String filePath) throws Exception 

Method Source Code

//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import android.util.Base64;

public class Main {
    private static final int CACHE_SIZE = 1024;

    public static String encodeFile(String filePath) throws Exception {
        byte[] bytes = fileToBytes(filePath);
        return encode(bytes);
    }//from  w ww  .java2  s.com

    public static byte[] fileToBytes(String filePath) throws Exception {
        byte[] data = new byte[0];
        File file = new File(filePath);
        if (file.exists()) {
            FileInputStream in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
            byte[] cache = new byte[CACHE_SIZE];
            int nRead = 0;
            while ((nRead = in.read(cache)) != -1) {
                out.write(cache, 0, nRead);
                out.flush();
            }
            out.close();
            in.close();
            data = out.toByteArray();
        }
        return data;
    }

    public static String encode(byte[] bytes) throws Exception {
        return new String(Base64.encode(bytes, Base64.DEFAULT));
    }
}

Related

  1. encodeFile(String filePath)
  2. binaryToBase64(File file)