how to convert image to base64 in android studio
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:src="@drawable/your_image" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/bitmapToBase64Btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Bitmap to Base64" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/base64ToBitmapBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Base64 to Bitmap" />
</LinearLayout>
MainActivity.java
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
private String base64String = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.imageView);
final AppCompatButton base64ToBitmap = findViewById(R.id.base64ToBitmapBtn);
final AppCompatButton bitmapToBase64 = findViewById(R.id.bitmapToBase64Btn);
bitmapToBase64.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// getting Bitmap from ImageView
final Bitmap imageBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
// converting to Base64
base64String = bitmapToBase64(imageBitmap);
// remove image from ImageView
imageView.setImageBitmap(null);
}
});
base64ToBitmap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// getting bitmap from base64 string
Bitmap bitmap = base64ToBitmap(base64String);
// setting bitmap to ImageView
imageView.setImageBitmap(bitmap);
}
});
}
private String bitmapToBase64(Bitmap bitmap) {
// initialize byte stream to store bytes from bitmap
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// compress Bitmap and getting bytes
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
// getting byte array from output stream
byte[] bytes = byteArrayOutputStream.toByteArray();
// get base64 encoded string
final String base64String = Base64.encodeToString(bytes, Base64.DEFAULT);
return base64String;
}
private Bitmap base64ToBitmap(String base64String) {
// decode base64 string
byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
// Initialize bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bitmap;
}
}
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
![Login-and-Register-Activity-Material-UI-design-in-Android-Studio](https://learnoset.com/Learnoset/Projects/LoginAndRegister05/login_register_05.png)
Login and Register Activity Material UI design in Android Studio
![HD-Wallpaper-App-in-Android-Studio-----------](https://learnoset.com/Learnoset/Projects/HDWallpaper/hd_wallpaper2_02.png)
HD Wallpaper App in Android Studio
![Programming-Quiz-App-for-Android-(Offline-Version)](https://learnoset.com/Learnoset/Projects/OflineQuiz/thumnail.jpg)
Programming Quiz App for Android (Offline Version)
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.