load Assets Text - Android android.content

Android examples for android.content:Asset

Description

load Assets Text

Demo Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.res.AssetManager;
import android.util.Log;

public class Main {

  private static final String TAG = "ActivityUtil";

  public static String loadAssetsText(Activity activity, String fileName, String charset) {
    return loadAssetsText(activity.getResources().getAssets(), fileName, charset);
  }/*from   w  w w  . j  a  va 2  s.  c  om*/

  public static String loadAssetsText(AssetManager assetManager, String fileName, String charset) {
    String text = null;
    try {
      BufferedReader br = new BufferedReader(new InputStreamReader(assetManager.open(fileName), charset));
      StringBuilder sb = new StringBuilder();
      while ((text = br.readLine()) != null) {
        sb.append(text);
      }
      text = sb.toString();
    } catch (IOException e) {
      Log.e(TAG, e.getMessage(), e);
    }
    return text;
  }

}

Related Tutorials