This example is targeting conversion of Base64 String to Image and representing that image on screen. The advantages of using Base64 string to image is the load times, especially if you are getting the image from server. The downside of using Base64 String is that the encoded file will be 137% of the size of original file. This action is good if you are consuming a lot of small from server and showing them rapidly.

What is Base64?

Base64 is used to transmit data over text based mediums where the original byte values of the data are important. Some systems may alter the original bytes of a string due to differing text encodings. Additionally, text based systems typically can’t handle full binary data. To be able to transmit a hash fingerprint for example, a receiver needs to be able to receive the exact same byte values sent by the originator. If the byte values were to change, verification of the digital signature would fail. You’ll find Base64 used in SSL certificates, SAML assertions for single sign on systems, embedding data in XML and HTML, as well as many other use cases. More for Base64 you can find here.


ButterKnife library

First we add our favorite ButterKnife library in our build.gradle app file and sync the project:


The Main Activity XML:

In this Activity, there is nothing more than to show the image on screen. Because of that we use only one defined ImageView

The Main Activity Java:

Also in the Main Activity we only show the image in previously defined imageView.

Dont forget to add ButterKnife.bind(this); method in onCreate.

Here we only call our static utility class Base64Util method – convertStringToBitmap() so we can show the image on screen.


The Base64Utill class:

Here is the actual conversion done. Here we have three methods: resizeBase64Image(), convertString64ToImage() and convertStringToBitmap. The first two are private, because we need them only to be available in the Base64Utill class, and nowhere else. The third one is public and it is entry point of using of this utility class.

The two class variables are defined for the height and width of the image in pixels.

Resize Base64 Image:

This methods receives String as a parameter and resizes the image if need to our defined will. We need encoded byte array deducted from the String image. After thet we need BitmapFactory.Options object so we can create the Bitmap image. Than we have control flow IF statement for exterminating if the image is smaller than our defined margins (IMG_HEIGHT and IMG_WIDTH). After, we scale the image to our desired size using IMG_HEIGHT and IMG_WIDTH which are 640 and 480 pixels.

Convert String to Bitmap

We need this method so we can convert the String into Bitmap so we can add this Bitmap to ImageView defined in the XML and used in MainActivity. of course this method returns Bitmap.

Convert String to Image

This method is public and we use it to access the Base64Utill class functionalities. It is quite simple. We send String to Base64Utill and we receive Bitmap.


This is the tool used to convert Base64 string to Image.

You can get the whole project on GitHub /Base64ToImage

Spread the love


Leave a Reply

Your email address will not be published.