Android decodes “base64 image “to “.png” image
Reference:
Convert base64 image data to an image file(.png) and save it to Local file system
Methods a
String base64Img = "";
// If "data:image/jpeg; Base64," prefix, just take the following part
if (base64Img.contains(",")) {
String[] base64ImgArray = base64Img.split(",");
if (base64ImgArray.length == 2) {
base64Img = base64ImgArray[1]; }}/ / decoding
try {
byte[] decodedString = Base64.decode(base64Img, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
if(mBitmapCreateListener ! =null) { mBitmapCreateListener.onBitmapCreate(decodedByte); }}catch (Exception e) {
e.printStackTrace();
// The callback failed to generate the image
if(mBitmapCreateListener ! =null) {
mBitmapCreateListener.onBitmapCreate(null); }}Copy the code
Method 2
FileOutputStream fos = null;
try {
if(base64ImageData ! =null) {
fos = context.openFileOutput("imageName.png", Context.MODE_PRIVATE);
byte[] decodedString = android.util.Base64.decode(base64ImageData, android.util.Base64.DEFAULT); fos.write(decodedString); fos.flush(); fos.close(); }}catch (Exception e) {
} finally {
if(fos ! =null) {
fos = null; }}Copy the code