How Do I Decode A Jpeg Image Encoded In Base64 In Android And See It On An ImageView?
My server sends a encoded Base64 string to my android device. After that, I decode the Base64 string in this method to make a drawable of it. I can't see the image when I add it in
Solution 1:
Convert your binary file to Base64 then use the following code to retrieve it:
public static void base64ToFile(String path, String strBase64)
throws IOException {
byte[] bytes = Base64.decode(strBase64);
byteArrayTofile(path, bytes);
}
public static void byteArrayTofile(String path, byte[] bytes)
throws IOException {
File imagefile = new File(path);
File dir = new File(imagefile.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(imagefile);
fos.write(bytes);
fos.close();
}
converting binary file to Base64:
public static String fileToBase64(String path) throws IOException {
byte[] bytes = fileToByteArray(path);
return Base64.encodeBytes(bytes);
}
public static byte[] fileToByteArray(String path) throws IOException {
File imagefile = new File(path);
byte[] data = new byte[(int) imagefile.length()];
FileInputStream fis = new FileInputStream(imagefile);
fis.read(data);
fis.close();
return data;
}
Post a Comment for "How Do I Decode A Jpeg Image Encoded In Base64 In Android And See It On An ImageView?"