Bitmap থেকে Byte Array কনভার্সন এবং обратно
এই উদাহরণটি দেখায় কীভাবে একটি Bitmap ইমেজকে বাইট অ্যারে-তে কনভার্ট করা যায় এবং আবার বাইট অ্যারে থেকে Bitmap তৈরি করা যায়। এটি ইমেজ সংরক্ষণ, নেটওয়ার্ক ট্রান্সমিশন, বা ডেটাবেজে স্টোর করার জন্য ব্যবহারী।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project থেকে প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট XML (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Convert Bitmap to Byte Array" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_marginTop="24dp"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="2">
<ImageView
android:id="@+id/ivSource"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_marginEnd="8dp"
android:scaleType="centerCrop"
android:contentDescription="Original Image" />
<ImageView
android:id="@+id/ivCompressed"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_marginStart="8dp"
android:scaleType="centerCrop"
android:contentDescription="Compressed Image" />
</LinearLayout>
</RelativeLayout>
ধাপ ৩: MainActivity.java বাস্তবায়ন
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
Button button;
ImageView ivSource, ivCompressed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
ivSource = findViewById(R.id.ivSource);
ivCompressed = findViewById(R.id.ivCompressed);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
convertBitmapToByteArray();
}
});
}
private void convertBitmapToByteArray() {
try {
// Assets ফোল্ডার থেকে ইমেজ লোড করুন
InputStream inputStream = getAssets().open("image.png");
Bitmap originalBitmap = BitmapFactory.decodeStream(inputStream);
if (originalBitmap == null) {
Toast.makeText(this, "Failed to load image from assets", Toast.LENGTH_SHORT).show();
return;
}
// মূল ইমেজ দেখান
ivSource.setImageBitmap(originalBitmap);
// Bitmap -> Byte Array (JPEG কমপ্রেশন সহ)
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// কোয়ালিটি: 0-100 (১০০ = বেস্ট কোয়ালিটি, কম কমপ্রেশন)
originalBitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] byteArray = stream.toByteArray();
// Byte Array -> Bitmap (ডিকমপ্রেস)
Bitmap compressedBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
// কমপ্রেসড ইমেজ দেখান
ivCompressed.setImageBitmap(compressedBitmap);
// তথ্য প্রদর্শন
String info = String.format("Original: %dx%d (%.1f KB)\nCompressed: %dx%d (%.1f KB)",
originalBitmap.getWidth(), originalBitmap.getHeight(), originalBitmap.getByteCount() / 1024f,
compressedBitmap.getWidth(), compressedBitmap.getHeight(), byteArray.length / 1024f);
Toast.makeText(this, "ByteArray created!\n" + info, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
Bitmap.CompressFormat অপশনগুলো
- JPEG: লসি কমপ্রেশন, ফটোগ্রাফির জন্য ভালো, ট্রান্সপারেন্সি সাপোর্ট করে না
- PNG: লসলেস কমপ্রেশন, ট্রান্সপারেন্সি সাপোর্ট করে, বড় ফাইল সাইজ
- WEBP: আধুনিক ফরম্যাট, উভয় লসি/লসলেস, ছোট ফাইল সাইজ (API 14+)
বড় ইমেজ হ্যান্ডলিং (OutOfMemoryError প্রতিরোধ)
// বড় ইমেজের জন্য inSampleSize ব্যবহার করুন
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
// ক্যালকুলেট ইনস্যাম্পল সাইজ
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
int inSampleSize = 1;
if (options.outHeight > reqHeight || options.outWidth > reqWidth) {
int halfHeight = options.outHeight / 2;
int halfWidth = options.outWidth / 2;
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
বাইট অ্যারে ফাইল হিসেবে সংরক্ষণ করা
// ইন্টারনাল স্টোরেজে সেভ
FileOutputStream fos = openFileOutput("image.jpg", Context.MODE_PRIVATE);
fos.write(byteArray);
fos.close();
// বা External Storage-এ (পারমিশন দরকার)
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "image.jpg");
FileOutputStream fos = new FileOutputStream(file);
fos.write(byteArray);
fos.close();
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<!-- এক্সটার্নাল স্টোরেজে লিখতে হলে -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
অ্যাপ্লিকেশন টেস্ট করুন
res/assets/image.pngফোল্ডার তৈরি করে একটি ছবি রাখুন- অ্যাপ রান করুন
- "Convert Bitmap to Byte Array" বাটনে ক্লিক করুন
- বামে মূল ছবি, ডানে কমপ্রেসড ছবি এবং টোস্টে ফাইল সাইজ তথ্য দেখাবে