How can I read files from the asset folder?

satya - Mon Aug 13 2012 13:38:47 GMT-0400 (Eastern Daylight Time)

Read my previous notes on using Resources

Read my previous notes on using Resources

satya - Mon Aug 13 2012 13:39:36 GMT-0400 (Eastern Daylight Time)

Here is the AssetManager API

Here is the AssetManager API

satya - Mon Aug 13 2012 13:44:21 GMT-0400 (Eastern Daylight Time)

How to read my android context variable through a static method

How to read my android context variable static method

Search for: How to read my android context variable static method

satya - Mon Aug 13 2012 13:44:51 GMT-0400 (Eastern Daylight Time)

Here is a link for stackoverflow

Here is a link for stackoverflow

satya - Mon Aug 13 2012 13:46:11 GMT-0400 (Eastern Daylight Time)

Key points


Store the application context in your app initialization
  and make it a static variable.
Your application context may be different from Activity context
Use volatile to deal with null values
You can use injection tools like RoboGuice

satya - Mon Aug 13 2012 15:24:24 GMT-0400 (Eastern Daylight Time)

You will need a context to read the assets

You will need a context to read the assets

satya - Mon Aug 13 2012 15:25:04 GMT-0400 (Eastern Daylight Time)

If you don't want to pass it around all the way you can use your singleton application object


public class MyApplication extends Application
{
   public final static String tag="MyApplication";
   
   //Make sure to check for null for this variable
   public static volatile Context m_appContext = null;

   @Override
   public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      Log.d(tag,"configuration changed");
   }

   @Override
   public void onCreate() {
      super.onCreate();
      Log.d(tag,"oncreate");
      MyApplication.m_appContext = this.getApplicationContext();
   }

   @Override
   public void onLowMemory() {
      super.onLowMemory();
      Log.d(tag,"onLowMemory");
   }

   @Override
   public void onTerminate() {
      super.onTerminate();
      Log.d(tag,"onTerminate");
   }

}

satya - Mon Aug 13 2012 15:25:52 GMT-0400 (Eastern Daylight Time)

You need to define this root object in your manifest


<application android:name=".MyApplication"
        android:icon="@drawable/icon"

satya - Mon Aug 13 2012 15:38:42 GMT-0400 (Eastern Daylight Time)

You can then do this in the bowels of your code


public String getStringFromAssetFile(String filename)
    {
       Context ctx = MyApplication.m_appContext; 
       if ( ctx == null)
       {
          throw new RuntimeException("Sorry your app context is null");
       }
       try
       {
         AssetManager am = ctx.getAssets();
         InputStream is = am.open(filename);
         String s = convertStreamToString(is);
         is.close();
         return s;
       }
       catch (IOException x)
       {
          throw new RuntimeException("Sorry not able to read filename:" + filename,x);
       }
    }
    
    //Optimize later. This may not be an efficient read
    private String convertStreamToString(InputStream is)
    throws IOException
    {   
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       int i = is.read();
       while (i != -1)
       {
          baos.write(i);
          i = is.read();
       }
       return baos.toString();
    }