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:
publicstaticvoidbase64ToFile(String path, String strBase64)throws IOException {
byte[] bytes = Base64.decode(strBase64);
byteArrayTofile(path, bytes);
}
publicstaticvoidbyteArrayTofile(String path, byte[] bytes)throws IOException {
Fileimagefile=newFile(path);
Filedir=newFile(imagefile.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStreamfos=newFileOutputStream(imagefile);
fos.write(bytes);
fos.close();
}
converting binary file to Base64:
publicstatic String fileToBase64(String path)throws IOException {
byte[] bytes = fileToByteArray(path);
return Base64.encodeBytes(bytes);
}
publicstaticbyte[] fileToByteArray(String path) throws IOException {
Fileimagefile=newFile(path);
byte[] data = newbyte[(int) imagefile.length()];
FileInputStreamfis=newFileInputStream(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?"