Encode and Decode Image in Base64 String in Android?

Ayush Soni
3 min readOct 31, 2022

--

Converting Image to Base64 String can help in many ways like if you want to encode and decode any image or can send it to API using a retrofit.

Sending Images as Strings to API is a better way to store image data in the database.

To convert the Image to Base64 String first take the Image from either gallery or camera. Here I am using the gallery in my previous post How to open camera in Android Studio you can get Images using the camera.

Steps to Make this App

Step 1 ➤

Let’s Make a Button and an ImageView in XML Button to open the Gallery and ImageView to show the captured Image and a TextView to show base64 String.

<!-- add Gallery Button to open the Gallery --><Buttonandroid:id="@+id/GalleryBtn"android:layout_width="100dp"android:layout_height="50dp"android:layout_marginLeft="140dp"android:text="Gallery" /><!-- add ImageView to display the image --><ImageViewandroid:id="@+id/Image"android:layout_width="350dp"android:layout_height="450dp"android:layout_marginLeft="15dp"android:layout_marginTop="70dp"android:layout_marginBottom="10dp" /><!-- Textview to display encoded text--><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:ellipsize="end"android:maxLines="5" />

Step 3➤

Navigate to app > Manifests > AndroidManifest.xml file and add the following permission to it

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Step 2 ➤

Now in the MainActivity file first We will instantiate the components made in the XML file (Gallery Button, ImageView, TextView) using the findViewById() method. And define the variable pic_id which is the request-id of the clicked image.

private static final int pic_id = 123

After that Use to bellow code to open the gallery.

GalleryBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"),pic_id);
}
});

To handle the result Override the onActivityResult().

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
...
...
}

In onActivityResult() if (resultCode == RESULT_OK && requestCode == REQUEST_ONE) then initialize Uri, bitmap and byte Stream then compress the Bitmap and initialize byte array then encode the image in String using edcodeToString method and show it in a TextView.

Congrats Now your image is encoded in the string you can do what you want to do with this.

// when result is ok// initialize uriUri uri=data.getData();// Initialize bitmaptry {Bitmap bitmap= MediaStore.Images.Media.getBitmap(getContentResolver(),uri);// initialize byte streamByteArrayOutputStream stream=new ByteArrayOutputStream();// compress Bitmapbitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);// Initialize byte arraybyte[] bytes=stream.toByteArray();// get base64 encoded stringString sImage= Base64.encodeToString(bytes,Base64.DEFAULT);// set encoded text on textviewtextView.setText(sImage);

This is how you encode an Image now using the bellow code you can decode this in the bitmap and show it in ImageView.

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
ImageView.setImageBitmap(decodedByte);

Android is King.

Thank you for your time and Bye Bye.

--

--