Android Asset Read getfromAssets(Context ctx, String file_name)

Here you can find the source of getfromAssets(Context ctx, String file_name)

Description

Read a file from assets

Return

the string from assets

Declaration


public static String getfromAssets(Context ctx, String file_name) 

Method Source Code

//package com.java2s;

import android.content.Context;

import android.content.res.AssetManager;

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**/*from   ww w .j a v  a2  s  . c o m*/
     * Read a file from assets
     *
     * @return the string from assets
     */

    public static String getfromAssets(Context ctx, String file_name) {

        AssetManager assetManager = ctx.getAssets();
        ByteArrayOutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            inputStream = assetManager.open(file_name);
            outputStream = new ByteArrayOutputStream();
            byte buf[] = new byte[1024];
            int len;
            try {
                while ((len = inputStream.read(buf)) != -1) {
                    outputStream.write(buf, 0, len);
                }
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
            }
        } catch (IOException e) {
        }
        return outputStream.toString();

    }
}

Related

  1. getFileFromAssets(Context context, String file)
  2. getImgFromAsserts(Context con, String filename)
  3. listAssetFiles(Context context, String path)
  4. assetAsByteArray(Resources res, String path)