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:

Read file 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 https://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

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

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