get File From Assets - Android App

Android examples for App:Assets File

Description

get File From Assets

Demo Code


//package com.java2s;
import android.content.Context;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static String geFileFromAssets(Context context, String fileName) {
        if (context == null || TextUtils.isEmpty(fileName)) {
            return null;
        }/* w  w w .j av a  2  s .  co  m*/
        StringBuilder s = new StringBuilder("");
        try {
            InputStreamReader in = new InputStreamReader(context
                    .getResources().getAssets().open(fileName));
            BufferedReader br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                s.append(line);
            }
            return s.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials