एंड्रॉइड में एसेट्स फ़ोल्डर से फाइलें पढ़ना
यह उदाहरण दर्शाता है कि `assets` फ़ोल्डर में संग्रहीत फाइलों को `InputStream` और कोटलिन एक्सटेंशन का उपयोग करके कैसे पढ़ा जाए।
चरण 1: नया प्रोजेक्ट बनाएं
एंड्रॉइड स्टूडियो में File → New Project चुनें।
चरण 2: एसेट्स फ़ोल्डर बनाएं और फाइल जोड़ें
- प्रोजेक्ट व्यू में `app` पर राइट-क्लिक → New → Folder → Assets Folder
- `main` Source Set चुनें → Finish
- `assets` फ़ोल्डर पर राइट-क्लिक → New → File → `myText.txt`
- फाइल में टेक्स्ट जोड़ें (उदाहरण: "यह एसेट्स फ़ोल्डर से पढ़ा गया एक नमूना टेक्स्ट है।")
चरण 3: लेआउट (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"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="16dp"
android:textAlignment="center"
android:textColor="@android:color/holo_purple"
android:textStyle="bold"
android:text="बटन दबाकर एसेट्स से टेक्स्ट पढ़ें" />
<Button
android:id="@+id/btnReadText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="30dp"
android:text="एसेट्स से टेक्स्ट पढ़ें"
android:textStyle="bold" />
</RelativeLayout>
चरण 4: MainActivity.kt
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import kotlin.io.use
class MainActivity : AppCompatActivity() {
private lateinit var button: Button
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
textView = findViewById(R.id.textViewContent)
button = findViewById(R.id.btnReadText)
button.setOnClickListener { readFromAssets() }
}
private fun readFromAssets() {
try {
// विधि 1: बफर्ड रीडर के साथ लाइन दर लाइन पढ़ें (बड़ी फाइलों के लिए कुशल)
val stringBuilder = StringBuilder()
assets.open("myText.txt").use { inputStream ->
BufferedReader(InputStreamReader(inputStream)).use { reader ->
reader.forEachLine { line ->
stringBuilder.appendLine(line)
}
}
}
textView.text = stringBuilder.toString()
Toast.makeText(this, "फाइल सफलतापूर्वक पढ़ी गई", Toast.LENGTH_SHORT).show()
} catch (e: IOException) {
textView.text = "त्रुटि: ${e.message}"
Toast.makeText(this, "फाइल पढ़ने में त्रुटि: ${e.message}", Toast.LENGTH_LONG).show()
e.printStackTrace()
}
}
// वैकल्पिक: एक बार में पूरी फाइल पढ़ें (छोटी फाइलों के लिए)
private fun readAllAtOnce() {
try {
val content = assets.open("myText.txt").use { inputStream ->
inputStream.readBytes().decodeToString()
}
textView.text = content
} catch (e: IOException) {
textView.text = "त्रुटि: ${e.message}"
}
}
}
चरण 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.readassets">
<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>
उन्नत: एसेट्स यूटिलिटी क्लास
import android.content.Context
import android.content.res.AssetManager
import java.io.IOException
import java.io.InputStream
import kotlin.io.use
object AssetsUtils {
// टेक्स्ट फाइल को स्ट्रिंग के रूप में पढ़ें
@Throws(IOException::class)
fun readTextFile(context: Context, fileName: String): String {
return context.assets.open(fileName).use { inputStream ->
inputStream.readBytes().decodeToString()
}
}
// टेक्स्ट फाइल को लाइनों की सूची के रूप में पढ़ें
@Throws(IOException::class)
fun readTextLines(context: Context, fileName: String): List {
return context.assets.open(fileName).use { inputStream ->
BufferedReader(InputStreamReader(inputStream)).use { reader ->
reader.readLines()
}
}
}
// JSON फाइल को पार्स करें (Gson/Moshi के साथ)
@Throws(IOException::class)
fun readJsonFile(context: Context, fileName: String, type: Type): T {
val json = readTextFile(context, fileName)
return com.google.gson.Gson().fromJson(json, type)
}
// एसेट्स में सभी फाइलें सूचीबद्ध करें
fun listAssets(context: Context, path: String = ""): Array {
return try {
context.assets.list(path)
} catch (e: IOException) {
emptyArray()
}
}
// रॉ इनपुट स्ट्रीम प्राप्त करें (छवियों, बाइनरी डेटा के लिए)
@Throws(IOException::class)
fun openAsset(context: Context, fileName: String): InputStream {
return context.assets.open(fileName)
}
// फाइल मौजूद है या नहीं जांचें
fun assetExists(context: Context, fileName: String): Boolean {
return try {
context.assets.open(fileName).use { it.close(); true }
} catch (e: IOException) {
false
}
}
}
// उपयोग:
val content = AssetsUtils.readTextFile(this, "myText.txt")
val lines = AssetsUtils.readTextLines(this, "data.csv")
val config = AssetsUtils.readJsonFile(this, "config.json", Config::class.java)
एसेट्स बनाम Raw बनाम Internal Storage:
| विशेषता | Assets | Raw (res/raw) | Internal Storage |
|---|---|---|---|
| पहुंच | `assets.open()` | `resources.openRawResource()` | `openFileInput()` |
| उप-निर्देशिका | समर्थित | समर्थित नहीं | समर्थित |
| कंपाइल-समय पहुंच | नहीं (स्ट्रिंग नाम) | हाँ (R.raw.filename) | नहीं |
| बिल्ड-टाइम संपीड़न | नहीं (डिफ़ॉल्ट) | नहीं | लागू नहीं |
| रनटाइम संशोधन | नहीं (केवल पढ़ने योग्य) | नहीं (केवल पढ़ने योग्य) | हाँ |
| उपयोग का मामला | कॉन्फ़िग, JSON, HTML, फॉन्ट, बड़ी फाइलें | छोटी मीडिया, ध्वनि | उपयोगकर्ता डेटा, कैश, डेटाबेस |
बड़ी फाइलों के लिए स्ट्रीमिंग:
// बड़ी फाइलों को मेमोरी में लोड किए बिना प्रोसेस करें
assets.open("large_file.json").use { inputStream ->
// JSON स्ट्रीमिंग पार्सर (Gson JsonReader, Moshi JsonReader)
val reader = JsonReader(InputStreamReader(inputStream))
reader.beginObject()
while (reader.hasNext()) {
val name = reader.nextName()
when (name) {
"items" -> {
reader.beginArray()
while (reader.hasNext()) {
// प्रत्येक आइटम को अलग-अलग प्रोसेस करें
processItem(reader)
}
reader.endArray()
}
else -> reader.skipValue()
}
}
reader.endObject()
}