Android Base64 File Encode binaryToBase64(File file)

Here you can find the source of binaryToBase64(File file)

Description

Encodes a file to a base 64 string

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Return

The encoded string

Declaration

public static String binaryToBase64(File file)
        throws FileNotFoundException, IOException 

Method Source Code

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

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

public class Main {
    /**/*from   w  w w . j  a  v a  2  s  .  c o m*/
     * Encodes a file to a base 64 string
     * @param file
     * @return The encoded string
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static String binaryToBase64(File file)
            throws FileNotFoundException, IOException {

        String base64String;
        RandomAccessFile raFile = new RandomAccessFile(file, "r");

        // Creating a bytes array with the video length
        long length = raFile.length();
        byte[] binaryVideo = new byte[(int) length];

        try {
            raFile.readFully(binaryVideo);
        } finally {
            // Closing stream read
            raFile.close();
        }

        // To base 64
        base64String = Base64.encodeToString(binaryVideo,
                (Base64.URL_SAFE + Base64.NO_WRAP));

        // Returning the video as a base 64 string
        return base64String;
    }
}

Related

  1. encodeFile(String filePath)
  2. encodeFile(String filePath)