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:mc.lib.assets.AssetsHelper.java

public static Drawable readImage(Context context, String path) {
    InputStream is = null;// w w w.  j a v  a  2s.c o m
    try {
        is = context.getAssets().open(path);
        return Drawable.createFromStream(is, null);
    } catch (IOException e) {
        Log.e(LOGTAG, "Can not read asset " + path, e);
    } finally {
        StreamHelper.close(is);
    }
    return null;
}

From source file:Main.java

public static File copyAssets(Context context, String fileName) {
    AssetManager assetManager = context.getAssets();
    File outFile = null;/*from w  w  w.  ja va  2 s .  c  o  m*/
    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open(fileName);
        // copy file path
        outFile = new File(context.getExternalFilesDir(null), 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

protected static Reader getJsonExample(Context mContext) throws IOException {
    //        return new InputStreamReader(mContext.getAssets().open("data.json"),"UTF-8");
    return new InputStreamReader(mContext.getAssets().open("data_05_10_2015.json"), "UTF-8");
}

From source file:Main.java

public static File copy1(Context context, String filename, String destfilename, ProgressDialog pd) {

    try {//from  w w w .j  a  v  a  2  s  .c  o  m
        InputStream in = context.getAssets().open(filename);
        int max = in.available();
        if (pd != null) {
            pd.setMax(max);
        }

        File file = new File(destfilename);
        OutputStream out = new FileOutputStream(file);
        byte[] byt = new byte[1024];
        int len = 0;
        int total = 0;
        while ((len = in.read(byt)) != -1) {
            out.write(byt, 0, len);
            total += len;
            if (pd != null) {
                pd.setProgress(total);
            }
        }
        out.flush();
        out.close();
        in.close();

        return file;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static String readFirstLine(Context context, String fileName) {
    BufferedReader reader = null;
    try {//  w ww.  j  a  v a  2s .c  om
        reader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));

        return reader.readLine();
    } catch (IOException e) {
        return "";
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

public static Bitmap loadBitmapAsset(Context context, String asset) {
    InputStream is = null;/* w w w . ja  va  2  s  .c  om*/
    Bitmap bitmap = null;
    try {
        is = context.getAssets().open(asset);
        if (is != null) {
            bitmap = BitmapFactory.decodeStream(is);
        }
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                Log.e(TAG, "Cannot close InputStream: ", e);
            }
        }
    }
    return bitmap;
}

From source file:Main.java

private static String getVerNameFromAssert(Context context) {

    String versionName = "";
    try {//  w w  w . j a  v  a2 s.c  o m
        Properties pro = new Properties();
        InputStream is = context.getAssets().open("channel.properties");
        pro.load(is);
        String tmpVersionName = pro.getProperty("versionName");

        versionName = new String(tmpVersionName.getBytes("ISO-8859-1"), "UTF-8");

        is.close();
        is = null;
    } catch (Exception e) {
        versionName = "";
        Log.e(TAG, "AppConfig.loadVersion have Exception e = " + e.getMessage());
    }
    return versionName;

}

From source file:Main.java

public static Typeface getTypefaceEn(Context context) {

    if (appTypefaceEn == null) {
        appTypefaceEn = Typeface.createFromAsset(context.getAssets(), "fonts/en_arial.ttf");
    }//from  w ww.  j av  a2s .  co m
    return appTypefaceEn;
}

From source file:Main.java

public static Typeface getTypeface33(Context context) {

    if (appTypeface33 == null) {
        appTypeface33 = Typeface.createFromAsset(context.getAssets(), "fonts/en33.otf");
    }//w ww . j  ava2s. c om
    return appTypeface33;
}

From source file:Main.java

public static String assetToSd(Context ctx, String inFileName, String outFileName) {

    try {//from   w w  w .  j a  v a 2  s . com
        InputStream is = ctx.getAssets().open(inFileName);
        if (is == null) {
            return null;
        }

        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            String sd = Environment.getExternalStorageDirectory().getAbsolutePath();
            String path = sd + "/uexTencent/";
            File filePath = new File(path);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            String fileName = path + outFileName;
            FileOutputStream fos = new FileOutputStream(fileName);

            byte[] buf = new byte[1024];
            int len = 0;
            int total = 0;
            while ((len = is.read(buf)) != -1) {
                fos.write(buf, 0, len);
                total++;
            }
            is.close();
            fos.close();
            fileSize = total;
            return fileName;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}