Convert image to base64 String and vice versa

Image to base64 String

To convert image to base64 String use the code given below.
1. Define a new ByteArrayOutputStream. baos = new;

2. Convert image in ImageView to Bitmap.
Bitmap bm = (( imageview1.getDrawable()).getBitmap();

Or convert image in drawable folder to Bitmap.
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);

3. Convert bitmap image to byte array and encode it to String.
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageString = android.util.Base64.encodeToString(imageBytes, android.util.Base64.DEFAULT);

Base64 String to image

1. Decode String to Byte array
byte[] imageBytes = android.util.Base64.decode(imageString, android.util.Base64.DEFAULT);

2. Convert Byte array to Bitmap
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);

3. Set the Bitmap as image of ImageView.

Here imageString is a base64 String.


  1. Base64 String to image doesn't work for me, image do not load

    1. This comment has been removed by the author.

  2. Im having issues loading images as well.

  3. I have figured out how to send and retrieve images to Firebase but it's showing the same image what am I missing? I'm trying to create a shopping application for a friend and this is a main part of the application, as it is sending a huge string of random numbers and integers.


    1. If you are Creating a shopping Application, you should use Android Studio or AIDE, and instead of converting image to String you should use Firebase Storage. You can get the codes for uploading image to Firebase Storage if you search google.

  4. Hello, I have a doubt it will be possible to upload a file like a pdf or other files to firebase as well images, I would like you to upload a video referring to that or I explained how to do it thanks.Greetings from Nicaragua.

  5. Online file converters are very useful and handy to your, take a look at this free online convert website.


Post a Comment

Popular posts from this blog

A Flash Light App in Sketchware

Create Music Player app in Sketchware

Simple car racing android game in Sketchware

In Sketchware: How to create a calculator for any simple formula?

Firebase auth in Sketchware for Login Activity