Assets Helper : AssetManager « Core Class « Android






Assets Helper

     
//package org.alldroid.forum.utils;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class AssetsHelper {
  private static final String  TAG      = AssetsHelper.class.getSimpleName ();

  private Context              context  = null;
  private AssetManager        assets  = null;

  public static AssetsHelper create ( Context context ) {
    return new AssetsHelper ( context );
  }

  private AssetsHelper ( Context context ) {
    this.setContext ( context );
    this.setAssets ( this.getContext ().getAssets () );
  }

  public StringBuilder read ( String asset ) {
    try {
      InputStream stream = getAssets ().open ( asset, AssetManager.ACCESS_BUFFER );
      int bytesRead = 0;
      byte[] buffer = new byte[256];
      StringBuilder data = new StringBuilder ();
      while ( (bytesRead = stream.read ( buffer, 0, buffer.length )) > 0 ) {
        data.append ( new String ( buffer, 0, bytesRead ) );
      }

      return data;
    } catch ( IOException ex ) {
      Log.e ( TAG, ex.getMessage (), ex );
    } catch ( Exception ex ) {
      Log.e ( TAG, ex.getMessage (), ex );
    }
    return null;
  }

  /**
   * @param context
   *          the context to set
   */
  public void setContext ( Context context ) {
    this.context = context;
  }

  /**
   * @return the context
   */
  public Context getContext ( ) {
    return context;
  }

  /**
   * @param assets
   *          the assets to set
   */
  public void setAssets ( AssetManager assets ) {
    this.assets = assets;
  }

  /**
   * @return the assets
   */
  public AssetManager getAssets ( ) {
    return assets;
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Open file with AssetManager
2.Read Asset Files