Android Convert Image to Base64 String or Base64 String to Image

In this tutorial you will learn how to convert image to base64 string or base64 string to image in android.

Base64 is an encoding schema that represents binary data in an ASCII string. It becomes really helpful in case you want to upload image to server or save the image in database.

How It Works?

Image to Base64 String

  • First convert the image into bitmap.
  • Then compress bitmap to ByteArrayOutputStream.
  • Convert ByteArrayOutputStream to byte array.
  • Finally convert byte array to base64 string.

Base64 String to Image

  • Convert the base64 string to byte array.
  • Now convert byte array to bitmap.

 

Android Convert Image to Base64 String or Base64 String to Image

Create new android studio project with package name com.base64example.

Add an image in res/drawable folder. This is the image that we will convert. See below screenshot I have added an image with name logo.png.

Android Convert Image to Base64 String or Base64 String to Image

Now add following code in respectively files.

acitivity_main.xml

 

MainActivity.java

Run the project.

I am converting the image to base64 string and then converting back to bitmap and finally showing it in ImageView.

 

Screenshot

Android Convert Image to Base64 String or Base64 String to Image

One thought on “Android Convert Image to Base64 String or Base64 String to Image

  1. sathesh

    Hello bro Neeraj Mishra, this is sathesh from Taiwan. I am a beginner in c++ programming. your service is extraordinary. So I want to join with you to learn to program in the easiest way.

    can you clear my doubts which are based on programming?

    I need your help to my studies…

    Thank you.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *