Android – Read file from Assets

By -
Problem: How to read files (Images or text files) from Assets folder?
Description:

I assume you are aware about File reading operation, but here we have question that how can we get a list of files which we have placed in Assets folder. Here AssetManager class can help you to access any files lying inside the Assets directory of android application ( (or any sub-folders inside the Assets directory).

For accessing files from Assets directory, you require object of the AssetManager class, which you can create by using getAssets() method:

AssetManager assetManager = getAssets();

Now, you can get the files by using list(String Path) method of AssetManager class:

String[] files = assetManager.list("");  // files from 'assets' directory
String[] files = assetManager.list("Files"); // files from 'assets/Files' directory

And go through example for the rest of the procedure. In the example, you can find code for fetching files name and displaying it, reading text file, open image file and creating drawable from it and displaying the same in ImageView.

Output:

TechnoTalkative - Read from Assets

Solution:

ReadFileAssetsActivity.java

package com.paresh.readfileasset;

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

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Paresh N. Mayani
 * @Website http://www.technotalkative.com
 */
public class ReadFileAssetsActivity extends Activity {

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		TextView txtContent = (TextView) findViewById(R.id.txtContent);
		TextView txtFileName = (TextView) findViewById(R.id.txtFileName);
		ImageView imgAssets = (ImageView) findViewById(R.id.imgAssets);

		AssetManager assetManager = getAssets();

		// To get names of all files inside the "Files" folder
		try {
			String[] files = assetManager.list("Files");

			for(int i=0; i<files.length; i++) 			{ 				txtFileName.append("\n File :"+i+" Name => "+files[i]);
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// To load text file
        InputStream input;
		try {
			input = assetManager.open("helloworld.txt");

	         int size = input.available();
	         byte[] buffer = new byte[size];
	         input.read(buffer);
	         input.close();

	         // byte buffer into a string
	         String text = new String(buffer);

	         txtContent.setText(text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// To load image
    	try {
	    	// get input stream
	    	InputStream ims = assetManager.open("android_logo_small.jpg");

	    	// create drawable from stream
	    	Drawable d = Drawable.createFromStream(ims, null);

	    	// set the drawable to imageview
	    	imgAssets.setImageDrawable(d);
    	}
    	catch(IOException ex) {
    		return;
    	}
	}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
	<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/txtContent">

	<ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/imgAssets">

	 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/txtFileName">
</TextView></ImageView></TextView></LinearLayout>

</Scrollview>

Download example: https://github.com/PareshMayani/Android-ReadFileAssets

Paresh Mayani

Paresh Mayani is a lead android developer from India. Currently, He is Sr. Software engineer at InfoStretch Solutions Pvt. Ltd. He has been developing android apps since very 1st version i.e. Android 1.5, he says he has seen days and nights of Android history. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

  • gaurav

    hello sir,

    thanks for the tutorial.

    But my problem is:

    I have a list view and onClick on an item from that list view i wast to show text respectively.

    for that i have added few .txt to asset folder and used switch case in onItemClick to read corrosponding text file.

    can you provide me some help to built it.

    In short i am builting a “book Shelf” app. So to read chapter wise i want the above prob to be solved.

    Thank you in advance.