Android File to Byte Array Read fileToBytes(String filePath)

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

Description

file To Bytes

Declaration

public static byte[] fileToBytes(String filePath) throws Exception 

Method Source Code

//package com.java2s;

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

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

    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();/*  w  w  w. j  a  v  a  2 s  .c  o m*/
            }
            out.close();
            in.close();
            data = out.toByteArray();
        }
        return data;
    }
}

Related

  1. readBytes(@NotNull File file)
  2. readBytes(@NotNull String filePath)
  3. readFileBytes(File fx)
  4. readFileToByteArray(File file)
  5. readFileToByteArray(String filePath)
  6. fileToByte(String filePath)
  7. getBytesFromFile(File file)
  8. readBytes(File file)
  9. readBytes(String path)