List of usage examples for android.content.res AssetManager open
public @NonNull InputStream open(@NonNull String fileName) throws IOException
From source file:Main.java
public static String getLocalizedAssetPath(AssetManager assetManager, String pathTemplate) { String path = String.format(Locale.US, pathTemplate, "-" + Locale.getDefault().getLanguage().toLowerCase()); try {//from w w w. ja va2 s. com InputStream is = assetManager.open(path); is.close(); } catch (Exception ex) { path = String.format(Locale.US, pathTemplate, ""); } return "file:///android_asset/" + path; }
From source file:Main.java
public static String loadData(AssetManager am) { String str;//from ww w .j a v a 2 s . com BufferedReader in = null; StringBuilder buf = null; try { buf = new StringBuilder(); InputStream json = am.open("treatment.json"); if (json != null) { in = new BufferedReader(new InputStreamReader(json)); while ((str = in.readLine()) != null) { buf.append(str); } } } catch (Exception e) { return ""; } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return buf.toString(); }
From source file:de.handtwerk.android.IoHelper.java
public static String readAssetAsString(String pPath, Context pContext) throws IOException { AssetManager assMan = pContext.getAssets(); InputStream is = assMan.open(pPath); return StringHelper.readStream(is); }
From source file:Main.java
public static InputStream loadAsset(Context context, String path, String filename) { AssetManager assetManager = context.getResources().getAssets(); InputStream inputStream = null; try {//from w ww. j av a2s . c o m String fullFilename = path + File.separator + filename; inputStream = assetManager.open(fullFilename); if (inputStream != null) Log.d("AssetsUtil", "Loaded " + fullFilename); } catch (IOException e) { e.printStackTrace(); } return inputStream; }
From source file:Main.java
public static File copyAssets(Context context, String fileName, File outFile) { AssetManager assetManager = context.getAssets(); InputStream in = null;//from w w w . ja v a2 s . c o m OutputStream out = null; try { in = assetManager.open(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.anhonesteffort.flock.sync.AppSecureSocketFactory.java
private static SSLContext createAppStoreSSLContext(Context appContext, boolean useFlockTrustStore) throws HttpClientError { if (appContext == null) throw new HttpClientError("application context is null :("); KeyStore trustStore;// w ww . j a v a 2 s.c o m try { if (useFlockTrustStore) { AssetManager assetManager = appContext.getAssets(); InputStream keyStoreInputStream = assetManager.open("flock.store"); trustStore = KeyStore.getInstance("BKS"); trustStore.load(keyStoreInputStream, "owsflock".toCharArray()); } else { trustStore = KeyStore.getInstance("AndroidCAStore"); trustStore.load(null, null); } TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509"); tmf.init(trustStore); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, tmf.getTrustManagers(), null); return sslContext; } catch (Exception e) { Log.e(TAG, "createAppStoreSSLContext() - flock store? " + useFlockTrustStore, e); throw new HttpClientError(e.toString()); } }
From source file:Main.java
public static void copyAsset(Context context, String assetFileName, String dstFile) { AssetManager assetManager = null; assetManager = context.getAssets();/*from w ww .j av a2 s. c o m*/ 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 String readFile(Activity activity, String fileName) { AssetManager assets = activity.getAssets(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int len;//from www . j a va2 s.c o m try { InputStream inputStream = assets.open(fileName); while ((len = inputStream.read(buf)) != -1) { outputStream.write(buf, 0, len); } outputStream.close(); inputStream.close(); return outputStream.toString(); } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:Main.java
private static Uri getUriFromAsset(Context mContext, String path) { File dir = mContext.getExternalCacheDir(); if (dir == null) { Log.e(TAG, "Missing external cache dir"); return Uri.EMPTY; }/*from w w w. j a v a2 s .com*/ String resPath = path.replaceFirst("file:/", "www"); String fileName = resPath.substring(resPath.lastIndexOf('/') + 1); String storage = dir.toString() + STORAGE_FOLDER; if (fileName == null || fileName.isEmpty()) { Log.e(TAG, "Filename is missing"); return Uri.EMPTY; } File file = new File(storage, fileName); FileOutputStream outStream = null; InputStream inputStream = null; try { File fileStorage = new File(storage); if (!fileStorage.mkdir()) Log.e(TAG, "Storage directory could not be created: " + storage); AssetManager assets = mContext.getAssets(); outStream = new FileOutputStream(file); inputStream = assets.open(resPath); copyFile(inputStream, outStream); outStream.flush(); outStream.close(); return Uri.fromFile(file); } catch (FileNotFoundException e) { Log.e(TAG, "File not found: assets/" + resPath); } catch (IOException ioe) { Log.e(TAG, "IOException occured"); } catch (SecurityException secEx) { Log.e(TAG, "SecurityException: directory creation denied"); } finally { try { if (inputStream != null) { inputStream.close(); } if (outStream != null) { outStream.flush(); outStream.close(); } } catch (IOException ioe) { Log.e(TAG, "IOException occured while closing/flushing streams"); } } return Uri.EMPTY; }
From source file:Main.java
/** * read a Text file from assets.//from w ww . ja v a2s .com * * @param context * @param fileName * @return Text * @throws IOException */ public static String readTextFromAsset(Context context, String fileName) throws IOException { AssetManager am = context.getResources().getAssets(); BufferedReader br = null; StringBuilder sb = new StringBuilder(); try { br = new BufferedReader(new InputStreamReader(am.open(fileName), "UTF-8")); } catch (UnsupportedEncodingException e) { // ignore. return null; } String str; while ((str = br.readLine()) != null) { sb.append(str + "\n"); } if (br != null) br.close(); return sb.toString(); }