Example usage for android.content.res AssetManager open

List of usage examples for android.content.res AssetManager open

Introduction

In this page you can find the example usage for android.content.res AssetManager open.

Prototype

public @NonNull InputStream open(@NonNull String fileName) throws IOException 

Source Link

Document

Open an asset using ACCESS_STREAMING mode.

Usage

From source file:Main.java

private static boolean copyDex(Context ctx, String dexAssertPath, String dexPath) {
    AssetManager assets = ctx.getAssets();
    InputStream inStream = null;//from w ww. ja  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

private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
    InputStream in = null;//from w w w. jav a2  s  .  co  m
    OutputStream out = null;
    try {
        in = assetManager.open(fromAssetPath);
        new File(toPath).createNewFile();
        out = new FileOutputStream(toPath);
        copyFile2(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

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   w w w  . j  a v a2s .  c o  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

public static String getAssetFileContent(Context context, String assetFileName) {
    AssetManager assetManager = context.getAssets();
    InputStream is = null;//from  ww w  .  j a  v a 2  s.co m
    BufferedReader reader = null;
    String lineSeparator = System.getProperty("line.separator");
    try {
        is = assetManager.open(assetFileName);
        reader = new BufferedReader(new InputStreamReader(is));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while (((line = reader.readLine()) != null)) {
            sb.append(line).append(lineSeparator);
        }
        return sb.toString();
    } catch (IOException e) {
    } finally {
        try {
            if (is != null) {
                is.close();
            }
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
        }
    }
    return null;
}

From source file:Main.java

public static File copyAssets(Context context, String fileName) {
    AssetManager assetManager = context.getAssets();
    File outFile = null;//from ww w  . ja  v  a  2 s .  c  om
    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:org.openmrs.client.utilities.FormsLoaderUtil.java

/**
 * Loads pre-defined forms, generated with OpenMRS XForm module,
 * from assets and saves them in database
 * @param manager/*from w w  w. j a v a 2 s  . co  m*/
 */
public static void loadDefaultForms(AssetManager manager) {
    InputStream inputStream = null;
    for (String formName : DEFAULT_FORMS) {
        try {
            inputStream = manager.open(FormsLoaderUtil.ASSET_DIR + formName + FormsLoaderUtil.XML_SUFFIX);
        } catch (IOException e) {
            OpenMRS.getInstance().getOpenMRSLogger().d(e.toString());
            OpenMRS.getInstance().getOpenMRSLogger().d("Failed to load form : " + formName);
        }
        FormsLoaderUtil.copyFormFromAssets(formName, inputStream);
    }

}

From source file:Main.java

public static String getText(Context context, String path) {
    AssetManager as = context.getResources().getAssets();

    InputStream is = null;/*from   w ww .ja  va2 s .  c  o m*/
    BufferedReader br = null;

    StringBuilder sb = new StringBuilder();
    try {
        try {
            is = as.open(path);
            br = new BufferedReader(new InputStreamReader(is));

            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str + "\n");
            }
        } finally {
            if (br != null)
                br.close();
        }
    } catch (IOException e) {
        return null;
    }

    return sb.toString();
}

From source file:org.cobaltians.cobalt.font.CobaltFontManager.java

private static String readFileFromAssets(String file) {
    try {//from   w  w w .  j  a  va  2  s .c  om
        AssetManager assetManager = mContext.getAssets();
        InputStream inputStream = assetManager.open(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder fileContent = new StringBuilder();
        int character;

        while ((character = bufferedReader.read()) != -1) {
            fileContent.append((char) character);
        }

        return fileContent.toString();
    } catch (FileNotFoundException exception) {
        if (Cobalt.DEBUG)
            Log.e(TAG, TAG + " - readFileFromAssets: " + file + "not found.");
    } catch (IOException exception) {
        if (Cobalt.DEBUG)
            Log.e(TAG, TAG + " - readFileFromAssets: IOException");
        exception.printStackTrace();
    }

    return "";
}

From source file:Main.java

public static Bitmap getImageBitmapFromAssetsFolderThroughImagePathName(Context context, String imagePathName,
        int reqWidth, int reqHeight) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;/*from  w w w.  j  a  v  a 2 s.com*/
    Bitmap bitmap = null;
    AssetManager assetManager = context.getResources().getAssets();
    InputStream inputStream = null;

    try {
        inputStream = assetManager.open(imagePathName);
        inputStream.mark(Integer.MAX_VALUE);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    }

    try {
        if (inputStream != null) {
            BitmapFactory.decodeStream(inputStream, null, opts);
            inputStream.reset();
        } else {
            return null;
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    opts.inSampleSize = calculateInSampleSiez(opts, reqWidth, reqHeight);
    //         Log.d(TAG,""+opts.inSampleSize);
    opts.inJustDecodeBounds = false;
    opts.inPreferredConfig = Bitmap.Config.RGB_565;
    opts.inPurgeable = true;
    opts.inInputShareable = true;
    opts.inDither = false;
    opts.inTempStorage = new byte[512 * 1024];
    try {
        if (inputStream != null) {
            bitmap = BitmapFactory.decodeStream(inputStream, null, opts);
        } else {
            return null;
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    //Log.d(TAG,"w:"+bitmap.getWidth()+" h:"+bitmap.getHeight());
    if (bitmap != null) {
        try {
            bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
        } catch (OutOfMemoryError outOfMemoryError) {
            outOfMemoryError.printStackTrace();
            System.gc();
            return null;
        }
    }
    return bitmap;
}

From source file:ca.psiphon.ploggy.Robohash.java

private static String loadAssetToString(AssetManager assetManager, String assetName) throws IOException {
    InputStream inputStream = null;
    try {//from   ww w.j av a  2s .  c  om
        inputStream = assetManager.open(new File(ASSETS_SUBDIRECTORY, assetName).getPath());
        return Utils.readInputStreamToString(inputStream);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}