Skip to content

Instantly share code, notes, and snippets.

@anta40
Created July 14, 2020 17:06
Show Gist options
  • Save anta40/55f92f6cdfbca95e0872af8216cf64af to your computer and use it in GitHub Desktop.
Save anta40/55f92f6cdfbca95e0872af8216cf64af to your computer and use it in GitHub Desktop.
Load a file and return its Base 64 value
public String fileToBase64(String path){
String base64 = "";
try {
File file = new File(path);
byte[] buffer = new byte[(int) file.length() + 100];
@SuppressWarnings("resource")
int length = new FileInputStream(file).read(buffer);
base64 = Base64.encodeToString(buffer, 0, length, Base64.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return base64;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment