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.
Now add following code in respectively files.
acitivity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.base64example.MainActivity"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/image"/> </RelativeLayout>
MainActivity.java
package com.base64example; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Base64; import android.widget.ImageView; import java.io.ByteArrayOutputStream; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView image =(ImageView)findViewById(R.id.image); //encode image to base64 string ByteArrayOutputStream baos = new ByteArrayOutputStream(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] imageBytes = baos.toByteArray(); String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT); //decode base64 string to image imageBytes = Base64.decode(imageString, Base64.DEFAULT); Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); image.setImageBitmap(decodedImage); } }
Run the project.
I am converting the image to base64 string and then converting back to bitmap and finally showing it in ImageView.
Screenshot
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.
i have load image form server side only image name how can encoding string please say
I have tried much of your solution but it is getting crashed when I add imageview dynamically to view
kindly review the problem in more detail here http://stackoverflow.com/questions/41955133/decode-and-show-image-thumbnail-in-messagebox-android-activity-firebase
it will be much appreciated , Thanks
encoded Image – base 64 string I want to save this table column as a Text entry but that’s length is too long, how to manage that, I am just saving as a string in table column and same fetching when dsiplaying.
Please suggest
You can use Blob column type instead of base64.
Thank you, and database will be upload in same format Base64 String
Hi when i try to convert image to base64 string format i can able to get only half part of base64 string data.can you please tell me how to get full base64 string data
Is it safe to encode/decode in main UI thread?
Is it possible to decode a text into bitmap and get image in another android app rather than decoding it in app where image encoded ?
how to convert base64 String into Drawable programatically???
I was getting base64 String for drawable resources but when i put the String on the Online Tool it doesn’t show the original image? Is there any Solution?
how can i put bitmap in array list and convert this array in base 64 string or pass it in server api???
Thanks, maybe I can help everyone with a simple jpg to png image converter at: jpg4png