reads the contents of the asset - Android App

Android examples for App:Assets

Description

reads the contents of the asset

Demo Code


//package com.java2s;
import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;
import android.content.Context;
import android.content.res.AssetManager;

public class Main {
    /**/*ww w .j  a v  a  2s  .c  o m*/
     * reads the contents of the asset
     * @param assetName
     * @return
     * @throws IOException 
     */
    public static String readAsset(Context context, String assetName) {
        try {
            AssetManager am = context.getAssets();
            InputStream in = am.open(assetName);
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(in));
            String sCurrentLine;
            StringBuffer contentBuffer = new StringBuffer();
            while ((sCurrentLine = bufferedReader.readLine()) != null) {
                contentBuffer.append(sCurrentLine);
            }
            return contentBuffer.toString();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }

    }
}

Related Tutorials