Android – Read file from Assets

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

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.


Read file from Assets


package com.paresh.readfileasset;


import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

 * @author Paresh N. Mayani
 * @Website
public class ReadFileAssetsActivity extends Activity {

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

	public void onCreate(Bundle savedInstanceState) {


		TextView txtContent = (TextView) findViewById(;
		TextView txtFileName = (TextView) findViewById(;
		ImageView imgAssets = (ImageView) findViewById(;

		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

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

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

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

		} catch (IOException e) {
			// TODO Auto-generated catch block

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

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

	    	// set the drawable to imageview
    	catch(IOException ex) {


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

<ScrollView xmlns: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">


Download example:

Paresh Mayani

Lazy android developer, exploring the horizon of android development since 7 years, currently working as a Technical Lead - Android at Simform Solutions, India. Former Application Architect at KarConnect. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...