First of all, let me give you a link: AssetManager, through this class we can easily access any files lying inside the Assets directory of android application. (or any sub-folders inside the Assets directory).

Now, we can have an object of AssetManager class by using getAssets() method:

AssetManager assetManager = getAssets();

And the rest of the procedure i have given and described by making comments in the example so now go through the full solutions provided below with the output snap.

Output:

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("

="" 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; } } } </files.length;>

main.xml

Note: Please consider scrollview as ScrollView, textview as TextView….etc. Its just problem inside the code plugin.

<!--?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 Full source code from here: Android – Read file from Assets

Reference: Android – Read file from Assets from our JCG partner Paresh N. Mayani at the TechnoTalkative blog.