Example usage for android.content Context getAssets

List of usage examples for android.content Context getAssets

Introduction

In this page you can find the example usage for android.content Context getAssets.

Prototype

public abstract AssetManager getAssets();

Source Link

Document

Returns an AssetManager instance for the application's package.

Usage

From source file:Main.java

public static String readDataFromAssets(String file, Context context) {
    try {/* ww w .ja  va2s. c  o  m*/
        InputStream is = context.getAssets().open(file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                // builder.append("\n"); // append a new line
            }
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return builder.toString();

    } catch (IOException e) {

        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String readAsset(Context context, String assetPath) throws IOException {
    String asset = null;//from  w  w  w. j  a  v  a  2  s  .c o  m
    AssetManager am = context.getAssets();
    try {
        InputStream is = am.open(assetPath);
        int length = is.available();
        byte[] data = new byte[length];
        is.read(data);
        is.close();
        asset = new String(data, "ASCII");
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return asset;
}

From source file:Main.java

public static String loadJSONFromAsset(final Context context, final String fileName) throws IOException {
    String json;// w  w  w  . j a v  a 2  s.c o m

    InputStream is = context.getAssets().open(fileName);
    int size = is.available();
    byte[] buffer = new byte[size];

    is.read(buffer);
    is.close();

    json = new String(buffer, "UTF-8");

    return json;
}

From source file:Main.java

public static Bitmap getBitmapFromAssert(Context mContext, String imgPath) {
    Bitmap bitmap = null;//  w w  w  .  j av a  2  s.  c o  m
    try {
        InputStream inputStream = mContext.getAssets().open(imgPath);
        bitmap = BitmapFactory.decodeStream(inputStream);
    } catch (IOException e) {
        return null;
    }
    return bitmap;
}

From source file:Main.java

public static void setDefaultFont(Context context, String path, String fontName) {
    try {//from w w  w.  j  a  va2s  .  c om
        defaultTypeFace = Typeface.createFromAsset(context.getAssets(), path + "/" + fontName + ".ttf");
    } catch (RuntimeException e) {
        if (e.getMessage().equals("native typeface cannot be made")) {
            try {
                throw new Exception("the fontName that you use does not exist in the file path!!");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static String LoadData(Context context, String inFile) {
    String tContents = "";
    try {//www.j a va2 s.  c  om
        InputStream stream = context.getAssets().open(inFile);
        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        tContents = new String(buffer);
    } catch (IOException e) {
        // Handle exceptions

    }

    return tContents;
}

From source file:Main.java

/**
 * get bitmap from assert//from   w w w.j  av a  2 s .c  o m
 * 
 * @param context
 * @param fileName
 * @return
 */
public static Bitmap getImageFromAssetFile(Context context, String fileName) {
    Bitmap image = null;
    try {
        AssetManager am = context.getAssets();
        InputStream is = am.open(fileName);
        image = BitmapFactory.decodeStream(is);
        is.close();
    } catch (Exception e) {

    }
    return image;
}

From source file:ee.ioc.phon.android.inimesed.MyFileUtils.java

public static void copyAssets(Context context, File baseDir) throws IOException {
    AssetManager assetManager = context.getAssets();
    String[] files = assetManager.list("hmm");

    for (String fromFile : files) {
        File toFile = new File(baseDir.getAbsolutePath() + "/" + fromFile);
        Log.i(LOG_TAG, "Copying: " + fromFile + " to " + toFile);
        InputStream in = assetManager.open("hmm/" + fromFile);
        FileUtils.copyInputStreamToFile(in, toFile);
    }/*  w w  w.  j a  v  a2  s  . com*/
}

From source file:Main.java

public static Bitmap converFileToBitMapWithFilePath(Context context, String filePath) {

    Bitmap result = null;/*ww w.  j a v a 2s  .  c  o m*/

    try {

        InputStream is = context.getAssets().open(filePath);
        result = BitmapFactory.decodeStream(is);

    } catch (IOException e) {

    }

    return result;
}

From source file:Main.java

/**
 * To get all file inside folder/* w  ww  . j a v a  2s.c  o m*/
 * 
 * @return String[]
 */
public static String[] getFileName(Context context, String folderName) {
    String[] files = null;
    try {
        AssetManager assetManager = context.getAssets();
        files = assetManager.list(folderName);
    } catch (IOException e1) {
    }
    return files;
}