यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android में इमेज (ड्रॉएबल रिसोर्स) को Base64 एन्कोडेड स्ट्रिंग में कैसे कन्वर्ट किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: activity_main.xml में TextView जोड़ें
<?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"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:textSize="12sp"
android:textColor="@android:color/background_dark"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: MainActivity.kt में Base64 कन्वर्जन लॉजिक
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Base64
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.io.ByteArrayOutputStream
class MainActivity : AppCompatActivity() {
lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
textView = findViewById(R.id.textView)
val byteArrayOutputStream = ByteArrayOutputStream()
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.image)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
val imageBytes: ByteArray = byteArrayOutputStream.toByteArray()
val imageString: String = Base64.encodeToString(imageBytes, Base64.DEFAULT)
textView.text = imageString
}
}
चरण 4: Base64 स्ट्रिंग को वापस इमेज में डिकोड करना
fun decodeBase64ToBitmap(base64String: String): Bitmap? {
val decodedBytes = Base64.decode(base64String, Base64.DEFAULT)
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size)
}
// उपयोग:
val bitmap = decodeBase64ToBitmap(imageString)
imageView.setImageBitmap(bitmap)
चरण 5: AndroidManifest.xml कॉन्फ़िगर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.q11">
<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>
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर Base64 स्ट्रिंग देखें।
उपयोग के मामले:
- इमेज को JSON/API में भेजना
- डेटाबेस में इमेज स्टोर करना
- इमेज को इंटेंट के माध्यम से पास करना
- ऑफलाइन कैशिंग
नोट: Base64 एन्कोडिंग फ़ाइल आकार को ~33% बढ़ा देती है। बड़ी इमेजेज के लिए कंप्रेशन क्वालिटी (100 → 80) कम करें।