List of usage examples for android.content.res AssetManager open
public @NonNull InputStream open(@NonNull String fileName) throws IOException
From source file:Main.java
/** * Loads a file into string/* w w w. ja va2 s . com*/ */ public static String readFile(String fileName, AssetManager assetManager) throws IOException { InputStream input; input = assetManager.open(fileName); int size = input.available(); byte[] buffer = new byte[size]; input.read(buffer); input.close(); return new String(buffer); }
From source file:Main.java
/** * @param filePath example("text/text_1.txt") *//*from w w w . j a va 2 s.c o m*/ public static InputStreamReader getAssetsText(Context context, String filePath) { InputStreamReader reader = null; AssetManager assetManager = context.getAssets(); try { InputStream inputStream = assetManager.open(filePath); reader = new InputStreamReader(inputStream, "utf-8"); } catch (IOException e) { e.printStackTrace(); } return reader; }
From source file:Main.java
/** * Load an image from an asset file./* w w w. j av a 2s . c o m*/ * * @param assetManager * @param imageAssetPath Path to image * @return image object * @throws IOException */ public static Bitmap loadBitmapAsset(AssetManager assetManager, String imageAssetPath) throws IOException { return BitmapFactory.decodeStream(assetManager.open(imageAssetPath)); }
From source file:Main.java
/** * @param filePath example("pictures/picture_1.jpg") *///from w ww . j a v a 2 s .c o m public static Bitmap getAssetsPicture(Context context, String filePath) { Bitmap bitmap = null; AssetManager assetManager = context.getAssets(); try { InputStream inputStream = assetManager.open(filePath); bitmap = BitmapFactory.decodeStream(inputStream); } catch (IOException e) { e.printStackTrace(); } return bitmap; }
From source file:Main.java
public static String copyAsset(Context context, String assetName, File dir) { try {//from ww w .j a v a 2 s. c o m if (!dir.exists()) { dir.mkdirs(); } File outFile = new File(dir, assetName); if (!outFile.exists()) { AssetManager assetManager = context.getAssets(); InputStream in = assetManager.open(assetName); OutputStream out = new FileOutputStream(outFile); copyFile(in, out); in.close(); out.close(); } return outFile.getAbsolutePath(); } catch (Exception e) { } return ""; }
From source file:Main.java
private static boolean copyFile(File assetFile, File storageFile, AssetManager assetManager) { try {/*from w w w . ja va2 s .c o m*/ InputStream in = assetManager.open(assetFile.getPath()); FileOutputStream out = new FileOutputStream(storageFile); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); out.close(); return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } }
From source file:Main.java
public static Map<String, List<String>> createDictionary(Context context) { try {/* w w w . j a va 2 s.co m*/ AssetManager am = context.getAssets(); InputStream is = am.open(DICTIONARY_FILENAME); Scanner reader = new Scanner(is); Map<String, List<String>> map = new HashMap<String, List<String>>(); while (reader.hasNextLine()) { String word = reader.nextLine(); char[] keyArr = word.toCharArray(); Arrays.sort(keyArr); String key = String.copyValueOf(keyArr); List<String> wordList = map.get(key); if (wordList == null) { wordList = new LinkedList<String>(); } wordList.add(word); map.put(key, wordList); } reader.close(); return map; } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } return null; }
From source file:Main.java
/** * @param context/*from w ww. ja v a2 s . com*/ * @param filePath file path relative to assets, like request_init1/search_index.json * @return */ public static String readAssert(Context context, String filePath) { try { if (filePath.startsWith(File.separator)) { filePath = filePath.substring(File.separator.length()); } AssetManager assetManager = context.getAssets(); InputStream inputStream = assetManager.open(filePath); DataInputStream stream = new DataInputStream(inputStream); int length = stream.available(); byte[] buffer = new byte[length]; stream.readFully(buffer); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byteArrayOutputStream.write(buffer); stream.close(); return byteArrayOutputStream.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Read a text file from assets/*w w w .ja v a 2 s .c o m*/ * * @param context * @param fileName * @return */ public static String read(Context context, String fileName) { try { AssetManager am = context.getAssets(); InputStream in = am.open(fileName); // String pckg = context.getPackageName(); // ContentResolver cr = context.getContentResolver(); // Uri uri = Uri.parse("android.resource://"+pckg+"/"+fileName); // InputStream in = cr.openInputStream(uri); String txt = read(new InputStreamReader(in)); return txt; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static Bitmap getImageFromAssetsFile(Context context, String fileName) { Bitmap image = null;//from w w w. j av a2 s . co m AssetManager am = context.getAssets(); InputStream is = null; try { is = am.open(fileName); image = BitmapFactory.decodeStream(is); return image; } catch (IOException e) { e.printStackTrace(); return image; } finally { if (is != null) { try { is.close(); is = null; } catch (IOException e) { e.printStackTrace(); } } } }