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 Typeface getTypefaceZh(Context context) {

    if (appTypefaceZh == null) {
        appTypefaceZh = Typeface.createFromAsset(context.getAssets(), "fonts/en53.otf");
        ;//Typeface.createFromAsset (context.getAssets() , "fonts/zh.otf");
    }/*from  www.j a v a2s . c om*/
    return appTypefaceZh;
}

From source file:Main.java

public static boolean hasAssetFile(Context c, String filePath) {
    boolean hasFile = false;
    InputStream inputStream = null;
    try {//from ww  w .  j a  v a  2 s  .  c  o  m
        AssetManager am = c.getAssets();
        inputStream = am.open(filePath);
        hasFile = true;
    } catch (Exception e) {
        e.printStackTrace();
        hasFile = false;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return hasFile;
}

From source file:Main.java

public static Bitmap getFromAsserts(Context context, String assertName) {
    InputStream inputStream = null;
    Bitmap bitmap = null;/*  ww  w  .j a va  2  s  . c o  m*/
    try {
        inputStream = context.getAssets().open(assertName);
        bitmap = BitmapFactory.decodeStream(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return bitmap;
        }
    }

}

From source file:Main.java

public static File copyAssets(Context context, String fileName, File outFile) {
    AssetManager assetManager = context.getAssets();
    InputStream in = null;//from   www. jav a 2s  .  c  o m
    OutputStream out = null;
    try {
        in = assetManager.open(fileName);
        out = new FileOutputStream(outFile);
        copyFile(in, out);
    } catch (IOException e) {
        Log.e("tag", "Failed to copy asset file: " + fileName, e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // NOOP
            }
        }
        if (out != null) {
            try {
                out.close();

            } catch (IOException e) {
                // NOOP
            }
        }

    }
    return outFile;
}

From source file:Main.java

public static byte[] readAsset(Context context, String filename) throws IOException {
    InputStream in = context.getAssets().open(filename);
    try {//from   www  .  j  a  va 2 s  .c o m
        return readAllBytes(in);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static String readFile(Context context, String file, String code) {
    int len = 0;/*from   www.  j a v  a  2s .c  o m*/
    byte[] buf = null;
    String grammar = "";
    try {
        InputStream in = context.getAssets().open(file);
        len = in.available();
        buf = new byte[len];
        in.read(buf, 0, len);

        grammar = new String(buf, code);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return grammar;
}

From source file:Main.java

public static File extractAssetToFile(Context context, String file) {
    File cacheFile = new File(context.getCacheDir(), file);
    try {/*from   ww  w.  ja  va2s  . c o  m*/
        InputStream inputStream = context.getAssets().open(file);
        try {
            FileOutputStream outputStream = new FileOutputStream(cacheFile);
            try {
                byte[] buf = new byte[1024];
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    outputStream.write(buf, 0, len);
                }
            } finally {
                outputStream.close();
            }
        } finally {
            inputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return cacheFile;
}

From source file:Main.java

public static File fileFromAsset(Context context, String assetName) throws IOException {
    File outFile = new File(context.getCacheDir(), assetName + "-pdfview.pdf");
    copy(context.getAssets().open(assetName), outFile);
    return outFile;
}

From source file:Main.java

public static String readAssets(Context context, String fileName) {
    InputStream is = null;/*from ww w. ja va  2 s.  co m*/
    String content = null;
    try {
        is = context.getAssets().open(fileName);
        if (is != null) {

            byte[] buffer = new byte[1024];
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            while (true) {
                int readLength = is.read(buffer);
                if (readLength == -1)
                    break;
                arrayOutputStream.write(buffer, 0, readLength);
            }
            is.close();
            arrayOutputStream.close();
            content = new String(arrayOutputStream.toByteArray());

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        content = null;
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return content;
}

From source file:Main.java

/**
 *
 * GetAssetsFile allows you to open a file that exists in the Assets directory.
 *
 * @param context/*from w  w w.  j  a va2s  .c  om*/
 * @param fileName
 * @return the contents of the file.
 */
public static String getAssetsFile(Context context, String fileName) {
    try {
        String file = "";
        BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));

        // do reading
        String line = "";
        while (line != null) {
            file += line;
            line = reader.readLine();
        }

        reader.close();
        return file;

    } catch (Exception e) {
        return "";
    }
}