Android read a file from assets in java/Kotlin

This article is about how to read a file from the assets folder in android in both languages Java/Kotlin.

Creating Assets folder and a file

1. To create an asset folder in Android studio open your project in Android mode. Go to the app > right-click > New > Folder > Assets Folder.

 

2. Android Studio will open a dialog box. Under the Target Source Set, option main should be selected, and click on the finish button.

3. Right-click on the assets folder, select New >> file (myText.txt), and add your text.

myText.txt

"All our dreams can come true if we have the courage to pursue them"

Creating the XML file

4 . The below layout file consists of a Button and a TextView.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_reading_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="@color/colorPrimaryDark"
        android:padding="15dp"
        android:text="Reading text from Assets"
        android:textColor="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:padding="15dp"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@+id/btn_reading_text"
        app:layout_constraintStart_toStartOf="@+id/btn_reading_text"
        app:layout_constraintTop_toBottomOf="@+id/btn_reading_text" />

</androidx.constraintlayout.widget.ConstraintLayout>

Implementing code in Activity

5 . In MainActivity, on-click of the button, we will read the text file (myText.txt) from the assets folder and convert it into a String, and then finally set it in a textview.

Java code:

MainActivity.java

package com.c1ctech.readassetsfileexp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    Button btnReadingText;
    TextView tvText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        tvText = findViewById(R.id.tv_text);

        btnReadingText = findViewById(R.id.btn_reading_text);
        btnReadingText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                readFromAsset();
            }
        });

    }

    //read file myText.txt from assets folder
    private void readFromAsset() {
        String string = "";

        try {
            InputStream inputStream = getAssets().open("myText.txt");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            string = new String(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }

        tvText.setText(string);
    }

}

Kotlin code:

MainActivity.kt

package com.c1ctech.readassetsfileexp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import java.io.IOException

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn_reading_text.setOnClickListener {
            readFromAsset()
        }
    }

    //read file myText.txt from assets folder
    private fun readFromAsset() {
        var string = ""
        try {
            val inputStream = assets.open("myText.txt")
            val size: Int = inputStream.available()
            val buffer = ByteArray(size)
            inputStream.read(buffer).toString()

            string = String(buffer)

        } catch (e: IOException) {
            e.printStackTrace()
        }
        tv_text.text = string
    }
}

Run the app and click on the button. You can see the file text displayed in the textview.

     

Leave a Reply