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:ch.ethz.coss.nervousnet.hub.ui.ShowcaseActivity.java

public static String parseJSONFile(String res, Context context) throws IOException {
    AssetManager manager = context.getAssets();
    InputStream file = manager.open(res);
    byte[] formArray = new byte[file.available()];
    file.read(formArray);//from w  ww.j  av  a  2  s. com
    file.close();

    return new String(formArray);
}

From source file:Main.java

static public void CopyAsset(Context ctx, File path, String filename) throws IOException {
    AssetManager assetManager = ctx.getAssets();
    InputStream in = null;//from www.  j a  v a 2  s  . co  m
    OutputStream out = null;

    // Copy files from asset folder to application folder
    try {
        in = assetManager.open(filename);
        out = new FileOutputStream(path.toString() + "/" + filename);
        copyFile(in, out);
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
        throw e;
    } finally {
        // Reclaim resources
        if (in != null) {
            in.close();
            in = null;
        }
        if (out != null) {
            out.flush();
            out.close();
            out = null;
        }
    }
}

From source file:Main.java

/**
 * @param filePath filePath example("pictures")
 *//*w w w . ja  v a2  s  .  c  o  m*/
public static List<String> traversalAssetsFiles(Context context, String filePath) {
    List<String> files = new ArrayList<>();
    try {
        for (String file : context.getAssets().list(filePath)) {
            if (file != null && !file.isEmpty()) {
                if (file.endsWith(".jpg") || file.endsWith(".gif") || file.endsWith(".png")) {
                    files.add(file);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return files;
}

From source file:Main.java

public static boolean copyAsset(Context context, String fromFile, String toFile) throws IOException {

    InputStream in = context.getAssets().open(fromFile);
    OutputStream out = new FileOutputStream(toFile);
    try {/*from   w  ww .j a  v a 2 s. c  o m*/
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    } finally {
        try {
            if (in != null)
                in.close();
            if (out != null)
                out.close();

        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static Drawable getDrawableAsset(Context context, String path) {

    // Get input stream
    InputStream inputStream = null;
    try {//from ww  w .j a v a 2 s.c  o  m
        inputStream = context.getAssets().open(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Load image as Drawable
    Drawable drawable = Drawable.createFromStream(inputStream, null);

    return drawable;
}

From source file:Main.java

public static void copyAsset(Context context, String assetFileName, String dstFile) {
    AssetManager assetManager = null;//from  ww  w .  ja va 2s  .c  o  m
    assetManager = context.getAssets();
    InputStream inputStream = null;
    FileOutputStream fileOutputStream = null;

    try {
        inputStream = assetManager.open(assetFileName);
        fileOutputStream = new FileOutputStream(dstFile);
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        fileOutputStream.write(buffer);
        fileOutputStream.flush();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            inputStream.close();
            fileOutputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

From source file:Main.java

public static Bitmap getEmotion(Context context, String key) {
    String fileName = "default_emotions_package/" + MAP.get(key);
    try {//from   w  ww.j av a 2  s .c o  m
        AssetManager am = context.getAssets();
        return BitmapFactory.decodeStream(am.open(fileName));
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
    return null;
}

From source file:Main.java

private static boolean copyDex(Context ctx, String dexAssertPath, String dexPath) {
    AssetManager assets = ctx.getAssets();
    InputStream inStream = null;/* w ww . j a v  a2 s. co m*/
    OutputStream dexWriter = null;
    boolean suc = true;
    try {
        inStream = assets.open(dexAssertPath);
        FileOutputStream outStream = new FileOutputStream(dexPath);
        dexWriter = new BufferedOutputStream(outStream);
        byte[] buf = new byte[1024 * 8];
        int len;
        while ((len = inStream.read(buf)) > 0) {
            dexWriter.write(buf, 0, len);
        }
        dexWriter.close();
        inStream.close();
    } catch (Exception e) {
        e.printStackTrace();
        suc = false;
    } finally {
        try {
            if (null != inStream) {
                inStream.close();
            }
            if (null != dexWriter) {
                dexWriter.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return suc;
}

From source file:Main.java

/**
 * If private asset exists, load that, otherwise fall back to open source
 * assets.// w w w . jav  a  2s . c  o m
 * 
 * @param context
 * @param upgradePath
 * @return
 * @throws IOException
 */
public static InputStream openAsset(Context context, String relativePath) throws IOException {
    InputStream inputStream;
    try {
        inputStream = context.getAssets().open("private/" + relativePath);
    } catch (IOException e) {
        inputStream = context.getAssets().open("licensed/" + relativePath);
    }
    return inputStream;
}

From source file:Main.java

public static boolean checkIfDllInstalled(Context context) {
    boolean isInstalled = false;
    try {/*  w  ww .  j a va  2s . c  o m*/
        InputStream inputStream = context.getAssets().open("Disdll.dll");
        File file = context.getFileStreamPath("Disdll.dll");
        if (file != null) {
            if (file.length() == inputStream.available()) {
                isInstalled = true;
            }
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return isInstalled;
}