load Assets Text - Android android.content.res

Android examples for android.content.res:AssetManager

Description

load Assets Text

Demo Code

/*/*from w  ww.  j  av a 2 s .  c  om*/
 * Copyright (c) 2011 by KLab Inc., All rights reserved.
 *
 * Programmed by Naohide Sano
 */
//package com.java2s;
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);
    }

    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