Base64 स्ट्रिंग को Bitmap में डिकोड करने का तरीका
यह उदाहरण दर्शाता है कि Base64 एन्कोडेड स्ट्रिंग को वापस Bitmap इमेज में कैसे बदला जाए और ImageView में प्रदर्शित किया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (res/layout/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 in Android App"
android:textSize="16sp"
android:textStyle="bold|italic" />
</LinearLayout>
चरण 3: MainActivity.java
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);
// 1. ड्रॉएबल को Bitmap में बदलें
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
// 2. Bitmap को Base64 स्ट्रिंग में एन्कोड करें
byte[] imageBytes = byteArrayOutputStream.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
// 3. Base64 स्ट्रिंग को वापस Bitmap में डिकोड करें
byte[] decodedBytes = Base64.decode(imageString, Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
// 4. ImageView में सेट करें
imageView.setImageBitmap(decodedImage);
}
}
चरण 4: AndroidManifest.xml
<?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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
व्यावहारिक उपयोग
- सर्वर से Base64 स्ट्रिंग प्राप्त होने पर इमेज दिखाने के लिए।
- डेटाबेस/SharedPreferences में इमेज स्टोर करने के लिए।
- JSON API के माध्यम से इमेज ट्रांसफर के लिए।
नोट: बड़ी इमेज के लिए Base64 अक्षम है (साइज ~33% बढ़ जाता है)। प्रोडक्शन में URL या फाइल ट्रांसफर बेहतर है।