কম্পিউটার টিউটোরিয়াল

কোটলিন ব্যবহার করে অ্যান্ড্রয়েডে অ্যাসেটস থেকে ফাইল পড়ার টিউটোরিয়াল

এই উদাহরণটি প্রদর্শন করে কীভাবে অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে অ্যাসেটস ফোল্ডার থেকে ফাইল পড়া যায়।

পদক্ষেপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে যান File → New Project এবং প্রজেক্ট তৈরি করার জন্য প্রয়োজনীয় সব বিবরণ পূরণ করুন।

পদক্ষেপ ২: 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"> 
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="100dp" 
        android:layout_centerHorizontal="true" 
        android:text="Tutorials Point" 
        android:textAlignment="center" 
        android:textColor="@android:color/holo_green_dark" 
        android:textSize="32sp" 
        android:textStyle="bold" /> 
    <TextView 
        android:id="@+id/textView" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:padding="24dp" 
        android:textAlignment="center" 
        android:textColor="@android:color/holo_purple" 
        android:textStyle="bold" /> 
    <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="Read text from assets" 
        android:textStyle="bold" /> 
</RelativeLayout>

পদক্ষেপ ৩: অ্যাসেটস ফোল্ডার এবং ফাইল তৈরি করুন

  1. Right-click app → New → Folder → Assets Folder
  2. Assets ফোল্ডারে রাইট-ক্লিক → New → File → myText.txt
  3. ফাইলে আপনার পছন্দের টেক্সট লিখুন

পদক্ষেপ ৪: src/MainActivity.kt-এ কোড যোগ করুন

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader

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.textView)
        button = findViewById(R.id.btnReadText)

        button.setOnClickListener {
            readFromAssets()
        }
    }

    private fun readFromAssets() {
        try {
            assets.open("myText.txt").use { inputStream ->
                BufferedReader(InputStreamReader(inputStream)).use { reader ->
                    val stringBuilder = StringBuilder()
                    var line: String?
                    while (reader.readLine().also { line = it } != null) {
                        stringBuilder.append(line).append("\n")
                    }
                    textView.text = stringBuilder.toString()
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
            textView.text = "Error reading file: ${e.message}"
        }
    }
}
কোটলিন এক্সটেনশন ফাংশন সহ সহজ পদ্ধতি:
// build.gradle-এ যোগ করুন
// implementation "androidx.core:core-ktx:1.12.0"

import androidx.core.content.res.openTextFile

private fun readFromAssetsKtx() {
    try {
        val text = assets.openTextFile("myText.txt").readText()
        textView.text = text
    } catch (e: IOException) {
        textView.text = "Error: ${e.message}"
    }
}

পদক্ষেপ ৫: androidManifest.xml-এ কোড যোগ করুন

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="https://schemas.android.com/apk/res/android" 
    package="com.example.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 Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের রান আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।

  1. অ্যান্ড্রয়েডে Assets ফোল্ডার থেকে ফাইল পড়ার পদ্ধতি

  2. Android-এ strings.xml থেকে ভ্যালু পড়ার পদ্ধতি

  3. অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে গ্যালারি থেকে ছবি বেছে নেওয়ার উপায়

  4. অ্যান্ড্রয়েড থেকে পিসিতে ফাইল ট্রান্সফার: দ্রুত ও নিরাপদে ধাপে ধাপে গাইড