Monday 11 May 2015

Converting an Image to Base64 / Decoding Base64 back to bitmap In Android

Find the following methods which will do :

1. Convert the Image to base64 .

2. Convert Base64 string to Image (Bitmap).


1. Method to Decode base64 to bitmap

 private Bitmap decodeFromBase64ToBitmap(String encodedImage)

{

    byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);

    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

    return decodedByte;

    }

   

   

2. Method to Convert image to Base64 String.


    private String convertToBase64(String imagePath)

{

    Bitmap bm = BitmapFactory.decodeFile(imagePath);

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

   bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); 

    byte[] byteArrayImage = baos.toByteArray();

    String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

    return encodedImage;

    }

No comments:

Post a Comment