How to read file from asset folder in Android Studio using Kotlin code

Last updated Dec 20, 2021


In this example, we will see how to read file from asset folder and set into android widgets components using the Kotlin language.

Here we start the implementation.

IMPLEMENTATION:

Step 1: 
Create a new Project in android studio.

Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish

 

Step 2: Create a new assets folder by using

app > right-click > new > Folder > Assets Folder > FINISH.

 

Step 3: Create a new file in Notepad and paste the following code.

[
  {
    "userId": 1,
    "name": "Mohit"
  },
  {
    "userId": 2,
    "name": "Nikita"
  },
  {
    "userId": 3,
    "name": "John"
  },
  {
    "userId": 4,
    "name": "Partik"
  },
  {
    "userId": 5,
    "name": "Henna"
  },
  {
    "userId": 6,
    "name": "Hessal"
  }
]

and save it with name (data.json).

 

Step 4: Copy the file from your pc and paste inside Assets folder in Android Studio.

Read from Assets Output

 

Step 5: Now open the activity_main.xml file and add the following code.

   

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ScrollView
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            />

    </ScrollView>
</LinearLayout>

 

Step 6: Go to MainActivity.kt file and add the following code below setContentView(R.layout.activity_main.xml).

val data = readFromAsset()
        val textView = findViewById(R.id.textView)
        textView.setText(data)
    }
    private fun readFromAsset(): String {
        val file_name = "data.json"
        val bufferReader = application.assets.open(file_name).bufferedReader()
        val data = bufferReader.use {
            it.readText()
        }
        Log.d("Read", data)

        return data;
    }

 

Step 7: Run the app in your emulator or real device, you will get the output as given below:

OUTPUT:
 

Read from Assets Output

 

Complete Source Code of Read files from Assets Example:

activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ScrollView
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            />

    </ScrollView>
</LinearLayout>


 

MainActivity.kt file

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val data = readFromAsset()
        val textView = findViewById(R.id.textView)
        textView.setText(data)
    }
    private fun readFromAsset(): String {
        val file_name = "data.json"
        val bufferReader = application.assets.open(file_name).bufferedReader()
        val data = bufferReader.use {
            it.readText()
        }
        Log.d("Read", data)

        return data;
    }
}
 

 

Conclusion:  We have covered the topic of how to read file from assets folder in Android Studio using Kotlin.


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

793 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions