एंड्रॉइड में Bitmap और Drawable के बीच कन्वर्जन
यह उदाहरण दिखाता है कि `BitmapDrawable` क्लास का उपयोग करके Bitmap को Drawable में और Drawable को Bitmap में कैसे बदला जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट XML
res/layout/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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Bitmap को Drawable में बदलें" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btnConvert"
android:layout_marginTop="10dp"
android:scaleType="centerInside"
android:contentDescription="परिणाम इमेज" />
</RelativeLayout>
चरण 3: MainActivity में कन्वर्जन लॉजिक
MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private Button btnConvert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
btnConvert = findViewById(R.id.btnConvert);
btnConvert.setOnClickListener(v -> convertBitmapToDrawable());
}
private void convertBitmapToDrawable() {
// 1. ड्रॉएबल संसाधन से ड्रॉएबल प्राप्त करें
Drawable drawable = getResources().getDrawable(R.drawable.your_image, getTheme());
// 2. Drawable → Bitmap (यदि BitmapDrawable है)
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) drawable).getBitmap();
} else {
// वेक्टर ड्रॉएबल आदि के लिए बिटमैप बनाएं
bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
}
// 3. Bitmap → Drawable (BitmapDrawable)
Drawable newDrawable = new BitmapDrawable(getResources(), bitmap);
// 4. ImageView में सेट करें
imageView.setImageDrawable(newDrawable);
}
}
🔄 कन्वर्जन सारांश
| दिशा | कोड | नोट |
|---|---|---|
| Drawable → Bitmap | `((BitmapDrawable) drawable).getBitmap()` | केवल BitmapDrawable के लिए काम करता है |
| Drawable → Bitmap (सामान्य) | `Bitmap.createBitmap()` + `Canvas` | किसी भी Drawable के लिए काम करता है |
| Bitmap → Drawable | `new BitmapDrawable(resources, bitmap)` | सबसे सामान्य तरीका |
| Bitmap → Drawable (API 22+) | `BitmapDrawable.create(resources, bitmap)` | फैक्टरी मेथड |
⚠️ महत्वपूर्ण नोट्स
- म्यूटेबल बिटमैप: यदि बिटमैप को संशोधित करना है तो `bitmap.copy(Bitmap.Config.ARGB_8888, true)` उपयोग करें
- मेमोरी: बड़े बिटमैप को रिसाइकल करें: `bitmap.recycle()`
- थीम: `getDrawable(id, theme)` API 21+ के लिए उपयोग करें
- वेक्टर ड्रॉएबल: सीधे बिटमैप में कास्ट नहीं किया जा सकता, Canvas पर ड्रॉ करना पड़ता है
चरण 4: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें। "Bitmap को Drawable में बदलें" बटन दबाने पर इमेज ImageView में दिखाई देगी।