In this Android Example tutorial we will cover read file from assets folder and display content on the activity. To Read file from assets folder we will use AssetManager class. This class contains method open() to read content from the file.
Here we will pas the file name as attribute.
Let's get started
Step 1: Create android project in Android Studio
Step 2: Add Your JSON file inside assets folder
Assets folder structure should be like below
Step 3: Create UI to read and display JSON file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
>
<TextView
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="12sp"
android:textStyle="bold"/>
<Button
android:id="@+id/btnReadFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read text from assets"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:textStyle="bold">
</Button>
</RelativeLayout>
|
Step 4: Now Write function to Read File from Assets folder
fun readFile()
{
try {
val inputStream: InputStream = assets.open("assetsfile.txt")
val size: Int = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
string = String(buffer)
} catch (e: IOException) {
e.printStackTrace()
}
}
|
Read JSON File from Assets
fun readJSONFile()
{
try {
val inputStream: InputStream = assets.open("sample.json")
val size: Int = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
string = String(buffer)
} catch (e: IOException) {
e.printStackTrace()
}
}
|
Step 5: Now run application and tap on button, it will read file content from assets folder and display on TextView.
Complete Activity Code
package com.rrtutors.readjson
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.R.string
import android.widget.Button
import android.widget.TextView
import java.io.IOException
import java.io.InputStream
class ReadFileActivity : AppCompatActivity() {
lateinit var string:String
lateinit var display:TextView
lateinit var btnReadFile: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_read_file)
btnReadFile.setOnClickListener {
readJSONFile();
display.setText(string)
}
}
fun readFile()
{
try {
val inputStream: InputStream = assets.open("assetsfile.txt")
val size: Int = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
string = String(buffer)
} catch (e: IOException) {
e.printStackTrace()
}
}
fun readJSONFile()
{
try {
val inputStream: InputStream = assets.open("sample.json")
val size: Int = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
string = String(buffer)
} catch (e: IOException) {
e.printStackTrace()
}
}
}
|
Conclusion: In this Android example tutorial we covered how to read file from assets folder and display on the activity.