package com.baidu.idl.face.api.utils; import android.util.Base64; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class FileBase64Encoder { private byte[] buffer = new byte[24576]; private InputStream inputStream; public void setInputFile(File file) throws FileNotFoundException { this.inputStream = new FileInputStream(file); } public byte[] encode() { try { int read = this.inputStream.read(this.buffer); if (read != -1) { return Base64.encode(this.buffer, 0, read, 0); } closeInputStream(); return null; } catch (IOException e) { closeInputStream(); e.printStackTrace(); return null; } } private void closeInputStream() { try { this.inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } }