Base64 স্ট্রিং থেকে Bitmap তৈরি: এনকোড/ডিকোড পূর্ণ চক্র
এই উদাহরণটি দেখাবে কীভাবে একটি ইমেজকে Base64-এ এনকোড করে, তারপর সেটি আবার Bitmap-এ ডিকোড করে ImageView-তে দেখানো যায়। এটি সার্ভার থেকে Base64 স্ট্রিং পেলে ইমেজ দেখানোর জন্য অত্যন্ত গুরুত্বপূর্ণ।
পদক্ষেপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট শুরু করুন।
পদক্ষেপ ২: লেআউট XML (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Base64 string into a Bitmap image"
android:textSize="16sp"
android:textStyle="bold|italic" />
</LinearLayout>
পদক্ষেপ ৩: MainActivity.java - পূর্ণ চক্র (Encode → Decode)
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@SuppressLint("WrongThread")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
// ১. Drawable → Bitmap → Byte Array → Base64 String (Encode)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
originalBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String base64String = Base64.encodeToString(imageBytes, Base64.DEFAULT);
// ২. Base64 String → Byte Array → Bitmap (Decode) - মূল কাজ
byte[] decodedBytes = Base64.decode(base64String, Base64.DEFAULT);
Bitmap decodedBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
// ৩. ImageView-তে দেখান
imageView.setImageBitmap(decodedBitmap);
}
}
প্রয়োগিক উদাহরণ: সার্ভার থেকে Base64 স্ট্রিং পেলে
// রিট্রফিট/বলি রেসপন্স থেকে Base64 স্ট্রিং পাওয়া
String base64FromServer = response.body().getImageBase64();
// UI থ্রেড ব্লক না হয় সেজন্য ব্যাকগ্রাউন্ডে ডিকোড
new Thread(() -> {
byte[] decodedBytes = Base64.decode(base64FromServer, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
runOnUiThread(() -> imageView.setImageBitmap(bitmap));
}).start();
গুরুত্বপূর্ণ টিপস
- Base64.DEFAULT vs NO_WRAP: সার্ভার যদি লাইন ব্রেক না পাঠায় তবে DEFAULT ঠিক। NO_WRAP ব্যবহার করুন কাস্টম এনকোডারের সাথে।
- মেমরি সতর্কতা: বড় Base64 স্ট্রিং (৫এমবি+) OutOfMemoryError দিতে পারে। inSampleSize ব্যবহার করুন।
- কমপ্রেশন ফরম্যাট: PNG (lossless) vs JPEG (lossy)। JPEG ছোট ফাইল দেয় কিন্তু গুণমান কমায়।
- Data URL ফরম্যাট: যদি স্ট্রিং `data:image/png;base64,` দিয়ে শুরু হয়, তবে কমায়ের পরের অংশই ডিকোড করুন।
BitmapFactory.Options দিয়ে মেমরি অপ্টিমাইজেশন
BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length, options); // ইনস্যাম্পল সাইজ ক্যালকুলেট করুন (যেমন: ২x ছোট) options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; Bitmap optimizedBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length, options);