Android Asset Read getFileFromAssets(Context context, String file)

Here you can find the source of getFileFromAssets(Context context, String file)

Description

get File From Assets

Declaration

public static String getFileFromAssets(Context context, String file) 

Method Source Code

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

public class Main {
    public static String getFileFromAssets(Context context, String file) {
        String contents = "";
        BufferedReader reader = null;
        try {//www. ja v  a  2 s  . com
            reader = new BufferedReader(new InputStreamReader(context
                    .getAssets().open(file)));

            // do reading, usually loop until end of file reading  
            String mLine = reader.readLine();
            while (mLine != null) {
                contents += mLine;
                mLine = reader.readLine();
            }
        } catch (IOException e) {
            //log the exception
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    //log the exception
                }
            }
        }

        return contents;
    }
}

Related

  1. getImgFromAsserts(Context con, String filename)
  2. listAssetFiles(Context context, String path)
  3. getfromAssets(Context ctx, String file_name)
  4. assetAsByteArray(Resources res, String path)