List of usage examples for android.content Context getAssets
public abstract AssetManager getAssets();
From source file:Main.java
/** * @param context/*w w w . j a 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:com.keepassdroid.assets.TypefaceFactory.java
public static Typeface getTypeface(Context ctx, String fontPath) { Typeface tf;//from w w w . j ava 2 s .c om tf = (Typeface) typefaceMap.get(fontPath); if (tf != null) { return tf; } try { return Typeface.createFromAsset(ctx.getAssets(), fontPath); } catch (Exception e) { // Return null if we can't create it return null; } }
From source file:dk.dtu.imm.datacollector.MainPipeline.java
public static String getStringFromAsset(Context context, String filename) { InputStream is = null;/*from w ww .jav a2s .c o m*/ try { is = context.getAssets().open(filename); return IOUtils.inputStreamToString(is, Charset.defaultCharset().name()); } catch (IOException e) { Log.e(TAG, "Unable to read asset to string", e); return null; } finally { if (is != null) { try { is.close(); } catch (IOException e) { Log.e(TAG, "Unable to close asset input stream", e); } } } }
From source file:Main.java
public static void createFonts(Context context, String path) { fonts = new HashMap<>(); try {/*from w ww . j av a2 s. c o m*/ String[] listOfFonts = listAssetFiles(path, context); for (String s : listOfFonts) { Typeface typeface = Typeface.createFromAsset(context.getAssets(), path + "/" + s); fonts.put(s.substring(0, s.lastIndexOf(".")), typeface); Log.d("PKFontUtils", "fonts" + s.substring(0, s.lastIndexOf("."))); } } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static String readFileForAssert(Context context, String fileName) { BufferedReader br = null;//from w ww.ja v a2s.c om StringBuffer stringBuffer = new StringBuffer(); String tmp = ""; try { br = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName))); while ((tmp = br.readLine()) != null) { stringBuffer.append(tmp); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return stringBuffer.toString(); }
From source file:Main.java
public static BufferedReader readFile(Context context, String fileName) { BufferedReader br = null;/*from w w w.jav a 2 s . co m*/ StringBuffer stringBuffer = new StringBuffer(); try { br = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName))); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return br; }
From source file:org.adblockplus.libadblockplus.android.Utils.java
public static String readAssetAsString(Context context, String filename) throws IOException { BufferedReader in = null;// w ww. j a v a2 s. co m try { StringBuilder buf = new StringBuilder(); InputStream is = context.getAssets().open(filename); in = new BufferedReader(new InputStreamReader(is)); String str; boolean isFirst = true; while ((str = in.readLine()) != null) { if (isFirst) { isFirst = false; } else { buf.append('\n'); } buf.append(str); } return buf.toString(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { // ignored } } } }
From source file:Main.java
public static boolean copyFile(Context context, String assetPath, String sdcardPath) { InputStream is = null;//from www .j ava 2 s .com FileOutputStream fs = null; boolean result = false; try { int byteread = 0; is = context.getAssets().open(assetPath); fs = new FileOutputStream(sdcardPath); byte[] buffer = new byte[1024]; while ((byteread = is.read(buffer)) != -1) { fs.write(buffer, 0, byteread); } result = true; } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); if (fs != null) fs.close(); } catch (IOException e) { e.printStackTrace(); } } return result; }
From source file:Main.java
public static boolean copyAssetFile(Context context, String originFileName, String destFilePath, String destFileName) {// w ww . j a v a 2 s .com InputStream is = null; BufferedOutputStream bos = null; try { is = context.getAssets().open(originFileName); File destPathFile = new File(destFilePath); if (!destPathFile.exists()) { destPathFile.mkdirs(); } File destFile = new File(destFilePath + File.separator + destFileName); if (!destFile.exists()) { destFile.createNewFile(); } FileOutputStream fos = new FileOutputStream(destFile); bos = new BufferedOutputStream(fos); byte[] buffer = new byte[256]; int length = 0; while ((length = is.read(buffer)) > 0) { bos.write(buffer, 0, length); } bos.flush(); return true; } catch (Exception ex) { ex.printStackTrace(); } finally { if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != bos) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
From source file:Main.java
public final static String getAssetAsString(Context context, String asset) { Reader reader = null;/*from w ww. j av a2 s .c o m*/ StringBuilder sb = new StringBuilder(1024); try { reader = new InputStreamReader(context.getAssets().open(asset)); char[] buf = new char[1024]; int length; while ((length = reader.read(buf)) >= 0) sb.append(buf, 0, length); } catch (IOException e) { e.printStackTrace(); } finally { closeQuietly(reader); } String string = sb.toString(); return string; }