List of usage examples for android.content Context getAssets
public abstract AssetManager getAssets();
From source file:org.cyanogenmod.theme.chooser.AudiblePreviewFragment.java
private void loadThemeAudible(Context themeCtx, int type, PackageInfo pi) { AssetManager assetManager = themeCtx.getAssets(); String assetPath;// w w w. j av a 2 s . c o m switch (type) { case RingtoneManager.TYPE_ALARM: assetPath = "alarms"; break; case RingtoneManager.TYPE_NOTIFICATION: assetPath = "notifications"; break; case RingtoneManager.TYPE_RINGTONE: assetPath = "ringtones"; break; default: assetPath = null; break; } if (assetPath != null) { try { String[] assetList = assetManager.list(assetPath); if (assetList != null && assetList.length > 0) { AssetFileDescriptor afd = assetManager.openFd(assetPath + File.separator + assetList[0]); MediaPlayer mp = initAudibleMediaPlayer(afd, type); if (mp != null) { addAudibleToLayout(type, mp); } } } catch (IOException e) { mMediaPlayers.put(type, null); } } }
From source file:cn.org.eshow.framwork.util.AbFileUtil.java
/** * ???Assets./*from w w w . j a v a2 s.c om*/ * * @param context the context * @param name the name * @param encoding the encoding * @return the string */ public static String readAssetsByName(Context context, String name, String encoding) { String text = null; InputStreamReader inputReader = null; BufferedReader bufReader = null; try { inputReader = new InputStreamReader(context.getAssets().open(name)); bufReader = new BufferedReader(inputReader); String line = null; StringBuffer buffer = new StringBuffer(); while ((line = bufReader.readLine()) != null) { buffer.append(line); } text = new String(buffer.toString().getBytes(), encoding); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bufReader != null) { bufReader.close(); } if (inputReader != null) { inputReader.close(); } } catch (Exception e) { e.printStackTrace(); } } return text; }
From source file:com.mobiquitynetworks.cordova.mnnotifications.NotificationsManager.java
Properties readSDKProperties(Context ctx) { Properties properties = new Properties(); AssetManager assetManager = ctx.getAssets(); if (assetManager == null) return properties; try {//from w w w . j a va 2s . c o m InputStream is = assetManager.open("properties/mobiquity.properties"); properties.load(is); return properties; } catch (IOException ioe) { //Empty properties return properties; } }
From source file:jahirfiquitiva.iconshowcase.utilities.ZooperIconFontsHelper.java
public ZooperIconFontsHelper(Context context) { this.mAssetManager = context.getAssets(); if (context instanceof Callback) this.setCallback((Callback) context); }
From source file:org.chromium.ChromeI18n.java
private JSONObject getAssetContents(String assetName) throws IOException, JSONException { Context context = this.cordova.getActivity(); InputStream is = context.getAssets().open(assetName); //Small trick to get the scanner to pull the entire input stream in one go Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); String contents = s.hasNext() ? s.next() : ""; return new JSONObject(contents); }
From source file:edu.asu.msse.sgowdru.movieplus.MovieDescription.java
public void readFile(Context context) { String json = ""; String line;//from w w w.j a v a 2 s.c om try ( //Set reader to omdb.json BufferedReader reader = new BufferedReader( new InputStreamReader(context.getAssets().open("omdb.json")));) { while ((line = reader.readLine()) != null) { json += line; } Log.w(getClass().getSimpleName(), json); } catch (Exception e) { Log.w(getClass().getSimpleName(), "From exception"); e.printStackTrace(); } parseJson(json); }
From source file:com.google.fpl.liquidfunpaint.renderer.PhysicsLoop.java
private void createBackground(Context context) { // Read in our specific json file String materialFile = FileHelper.loadAsset(context.getAssets(), ParticleRenderer.JSON_FILE); try {// w w w. java2 s . c o m JSONObject json = new JSONObject(materialFile); // Texture for paper JSONObject materialData = json.getJSONObject(PAPER_MATERIAL_NAME); String textureName = materialData.getString(DIFFUSE_TEXTURE_NAME); mPaperTexture = new Texture(context, textureName); } catch (JSONException ex) { Log.e(TAG, "Cannot parse" + ParticleRenderer.JSON_FILE + "\n" + ex.getMessage()); } }
From source file:com.agateau.equiv.ui.Kernel.java
private void loadProductList(Context context) { InputStream stream;/*from w w w. j a v a2s. co m*/ mProductStore = new ProductStore(); try { stream = context.getAssets().open("products.csv"); } catch (IOException e) { throw new RuntimeException("Failed to open products.csv.", e); } try { ProductStoreCsvIO.read(stream, mProductStore, Product.Source.DEFAULT); } catch (IOException e) { throw new RuntimeException("Failed to read products.csv.", e); } try { stream = context.openFileInput(CUSTOM_PRODUCTS_CSV); } catch (FileNotFoundException e) { return; } try { ProductStoreCsvIO.read(stream, mProductStore, Product.Source.CUSTOM); } catch (IOException e) { // Do not throw an exception here, the app is still usable even if we failed to load custom products NLog.e("Failed to read custom products from %s: %s.", CUSTOM_PRODUCTS_CSV, e); } }
From source file:com.androidzeitgeist.dashwatch.common.IOUtil.java
public static InputStream openUri(Context context, Uri uri, String reqContentTypeSubstring) throws OpenUriException { if (uri == null) { throw new IllegalArgumentException("Uri cannot be empty"); }/*from w w w . j a v a 2 s. c om*/ String scheme = uri.getScheme(); if (scheme == null) { throw new OpenUriException(false, new IOException("Uri had no scheme")); } InputStream in = null; if ("content".equals(scheme)) { try { in = context.getContentResolver().openInputStream(uri); } catch (FileNotFoundException e) { throw new OpenUriException(false, e); } catch (SecurityException e) { throw new OpenUriException(false, e); } } else if ("file".equals(scheme)) { List<String> segments = uri.getPathSegments(); if (segments != null && segments.size() > 1 && "android_asset".equals(segments.get(0))) { AssetManager assetManager = context.getAssets(); StringBuilder assetPath = new StringBuilder(); for (int i = 1; i < segments.size(); i++) { if (i > 1) { assetPath.append("/"); } assetPath.append(segments.get(i)); } try { in = assetManager.open(assetPath.toString()); } catch (IOException e) { throw new OpenUriException(false, e); } } else { try { in = new FileInputStream(new File(uri.getPath())); } catch (FileNotFoundException e) { throw new OpenUriException(false, e); } } } else if ("http".equals(scheme) || "https".equals(scheme)) { OkHttpClient client = new OkHttpClient(); HttpURLConnection conn = null; int responseCode = 0; String responseMessage = null; try { conn = client.open(new URL(uri.toString())); conn.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT); conn.setReadTimeout(DEFAULT_READ_TIMEOUT); responseCode = conn.getResponseCode(); responseMessage = conn.getResponseMessage(); if (!(responseCode >= 200 && responseCode < 300)) { throw new IOException("HTTP error response."); } if (reqContentTypeSubstring != null) { String contentType = conn.getContentType(); if (contentType == null || contentType.indexOf(reqContentTypeSubstring) < 0) { throw new IOException("HTTP content type '" + contentType + "' didn't match '" + reqContentTypeSubstring + "'."); } } in = conn.getInputStream(); } catch (MalformedURLException e) { throw new OpenUriException(false, e); } catch (IOException e) { if (conn != null && responseCode > 0) { throw new OpenUriException(500 <= responseCode && responseCode < 600, responseMessage, e); } else { throw new OpenUriException(false, e); } } } return in; }
From source file:com.example.android.pdfrendererbasic.PdfRendererBasicFragment.java
/** * Sets up a {@link android.graphics.pdf.PdfRenderer} and related resources. *//* w ww .j a v a2s. c o m*/ private void openRenderer(Context context) throws IOException { // In this sample, we read a PDF from the assets directory. mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor(); // This is the PdfRenderer we use to render the PDF. mPdfRenderer = new PdfRenderer(mFileDescriptor); }