Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android code to convert base64 string to bitmap

Hi stackoverflow team i have a problem in converting base64 string to bitmap in android. I am using the camera to fetch the image and i am convert the image to base64 string to post to the server. I want to show that image in the imageview so how can i show the image in the ImageView after fetching the image from the camera. please help me to solve the problem.

like image 467
Amit Thaper Avatar asked Sep 27 '10 07:09

Amit Thaper


1 Answers

Assuming that your image data is in a String called myImageData, the following should do the trick:

    byte[] imageAsBytes = Base64.decode(myImageData.getBytes(), Base64.DEFAULT);
    ImageView image = (ImageView)this.findViewById(R.id.ImageView);
    image.setImageBitmap(
            BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
    );

For Base64 decoding, you can use http://iharder.sourceforge.net/current/java/base64/ as Android doesn't contain Base64-support prior to 2.2.

Note, I didn't actually run this code, so you'll have to doublecheck for errors.

like image 67
TuomasR Avatar answered Oct 05 '22 23:10

TuomasR