About Paresh Mayani

Paresh Mayani is a Mobile application developer from India, having been involved in Android app development since around 3 years. He writes technical articles at TechnoTalkative. Apart from his job, he manages Google Developer Group (GDG) - Ahmedabad and has been speaker in various events.

Android – Read file from Assets

Description:

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("\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;
     }
 }
}
</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.

Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

9 Responses to "Android – Read file from Assets"

  1. i need to read jar file like you read text file and image how to do that?

  2. ANKIT THAKUR says:

    1) can we delete the asset folder file at run time programtically

  3. ThemeBowl says:

    Hi,i have my files ie images on assets folder.consider i have 3 images at first and when i launch app i could see all 3 images loaded.but say i updated it with 3 more images and relaunch app.i couldn’t find recently added images in that app.i use shared preference to save app state.what should i do in order to make other 3 images visible?

  4. Akshay says:

    any jar file is needed for this application? because , space character is displaying as question mark.

  5. abhi says:

    I want to read data from an image/picture and save that data in some storage like xml/text/world/Data Base. Image may contain any form/Drivers License or may be some Credit card.

    Please advise.

  6. Vani says:

    Hi,

    I want to open PDF file from assets folder, i tried lots of but its not open, we can open text file but i want to open pdf file, also i got my pdf file name when i do

    String[] files = assetManager.list(“Files”);
    but its still not open
    please help me how can i open pdf file in read mode.

Leave a Reply


3 + = twelve



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books