List of usage examples for android.content Context getAssets
public abstract AssetManager getAssets();
From source file:org.catnut.util.CatnutUtils.java
/** * ?//w w w. j a va2 s. c o m * * @param boundPx the icon' s rectangle bound, if zero, use the default */ public static SpannableString text2Emotion(Context context, String key, int boundPx) { SpannableString spannable = new SpannableString(key); InputStream inputStream = null; Drawable drawable = null; try { inputStream = context.getAssets().open(TweetImageSpan.EMOTIONS_DIR + TweetImageSpan.EMOTIONS.get(key)); drawable = Drawable.createFromStream(inputStream, null); } catch (IOException e) { Log.e(TAG, "load emotion error!", e); } finally { closeIO(inputStream); } if (drawable != null) { if (boundPx == 0) { drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); } else { drawable.setBounds(0, 0, boundPx, boundPx); } ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE); spannable.setSpan(span, 0, key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } return spannable; }
From source file:ca.psiphon.ploggy.Robohash.java
public static Bitmap getRobohash(Context context, boolean cacheCandidate, byte[] data) throws Utils.ApplicationError { try {//from w w w .j a v a 2 s . c o m MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); byte[] digest = sha1.digest(data); String key = Utils.formatFingerprint(digest); Bitmap cachedBitmap = mCache.get(key); if (cachedBitmap != null) { return cachedBitmap; } ByteBuffer byteBuffer = ByteBuffer.wrap(digest); byteBuffer.order(ByteOrder.BIG_ENDIAN); // TODO: SecureRandom SHA1PRNG (but not LinuxSecureRandom) Random random = new Random(byteBuffer.getLong()); AssetManager assetManager = context.getAssets(); if (mConfig == null) { mConfig = new JSONObject(loadAssetToString(assetManager, CONFIG_FILENAME)); } int width = mConfig.getInt("width"); int height = mConfig.getInt("height"); JSONArray colors = mConfig.getJSONArray("colors"); JSONArray parts = colors.getJSONArray(random.nextInt(colors.length())); Bitmap robotBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas robotCanvas = new Canvas(robotBitmap); for (int i = 0; i < parts.length(); i++) { JSONArray partChoices = parts.getJSONArray(i); String selection = partChoices.getString(random.nextInt(partChoices.length())); Bitmap partBitmap = loadAssetToBitmap(assetManager, selection); Rect rect = new Rect(0, 0, width, height); Paint paint = new Paint(); paint.setAlpha(255); robotCanvas.drawBitmap(partBitmap, rect, rect, paint); partBitmap.recycle(); } if (cacheCandidate) { mCache.set(key, robotBitmap); } return robotBitmap; } catch (IOException e) { throw new Utils.ApplicationError(LOG_TAG, e); } catch (JSONException e) { throw new Utils.ApplicationError(LOG_TAG, e); } catch (NoSuchAlgorithmException e) { throw new Utils.ApplicationError(LOG_TAG, e); } }
From source file:watch.oms.omswatch.parser.OMSConfigDBParser.java
/** * Loads 'jsonconfig.json' file from Assets folder and returns as String. * // www. j a v a2 s. c o m * @param fileName * @return Json String * @throws IOException */ public static String LoadFile(String fileName, Context context) throws IOException { // Create a InputStream to read the file into InputStream inputstream; // get the file as a stream inputstream = context.getAssets().open(fileName); // create a buffer that has the same size as the InputStream byte[] buffer = new byte[inputstream.available()]; // read the text file as a stream, into the buffer inputstream.read(buffer); // create a output stream to write the buffer into ByteArrayOutputStream oS = new ByteArrayOutputStream(); // write this buffer to the output stream oS.write(buffer); // Close the Input and Output streams oS.close(); inputstream.close(); // return the output stream as a String return oS.toString(); }
From source file:it.crs4.most.ehrlib.WidgetProvider.java
/** * Parses the file to string./* w w w.j a v a 2 s. c om*/ * * @param context the context * @param filename the filename * @return the string */ public static String parseFileToString(Context context, String filename) { try { InputStream stream = context.getAssets().open(filename); int size = stream.available(); byte[] bytes = new byte[size]; stream.read(bytes); stream.close(); return new String(bytes); } catch (IOException e) { Log.i(TAG, "IOException: " + e.getMessage()); } return null; }
From source file:com.apptentive.android.sdk.util.Util.java
public static void replaceDefaultFont(Context context, String fontFilePath) { final Typeface newTypeface = Typeface.createFromAsset(context.getAssets(), fontFilePath); TypedValue tv = new TypedValue(); String staticTypefaceFieldName = null; Map<String, Typeface> newMap = null; Resources.Theme apptentiveTheme = context.getResources().newTheme(); ApptentiveInternal.getInstance().updateApptentiveInteractionTheme(apptentiveTheme, context); if (apptentiveTheme == null) { return;// w w w .ja v a2 s . co m } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (apptentiveTheme.resolveAttribute(R.attr.apptentiveFontFamilyDefault, tv, true)) { newMap = new HashMap<String, Typeface>(); newMap.put(tv.string.toString(), newTypeface); } if (apptentiveTheme.resolveAttribute(R.attr.apptentiveFontFamilyMediumDefault, tv, true)) { if (newMap == null) { newMap = new HashMap<String, Typeface>(); } newMap.put(tv.string.toString(), newTypeface); } if (newMap != null) { try { final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap"); staticField.setAccessible(true); staticField.set(null, newMap); } catch (NoSuchFieldException e) { ApptentiveLog.e("Exception replacing system font", e); } catch (IllegalAccessException e) { ApptentiveLog.e("Exception replacing system font", e); } } } else { if (apptentiveTheme.resolveAttribute(R.attr.apptentiveTypefaceDefault, tv, true)) { staticTypefaceFieldName = "DEFAULT"; if (tv.data == context.getResources().getInteger(R.integer.apptentive_typeface_monospace)) { staticTypefaceFieldName = "MONOSPACE"; } else if (tv.data == context.getResources().getInteger(R.integer.apptentive_typeface_serif)) { staticTypefaceFieldName = "SERIF"; } else if (tv.data == context.getResources().getInteger(R.integer.apptentive_typeface_sans)) { staticTypefaceFieldName = "SANS_SERIF"; } try { final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName); staticField.setAccessible(true); staticField.set(null, newTypeface); } catch (NoSuchFieldException e) { ApptentiveLog.e("Exception replacing system font", e); } catch (IllegalAccessException e) { ApptentiveLog.e("Exception replacing system font", e); } } } }
From source file:com.polyvi.xface.util.XFileUtils.java
/** * ?/*from w w w.j av a 2 s . c o m*/ * * @param filePath * [in] * * @return * */ public static boolean fileExists(Context context, String filePath) { if (null == filePath) { return false; } String absPath = null; if (filePath.startsWith(XConstant.ASSERT_PROTACAL)) { absPath = filePath.substring(XConstant.ASSERT_PROTACAL.length()); try { InputStream is = context.getAssets().open(absPath); if (is != null) { return true; } } catch (IOException e) { return false; } } else if (filePath.startsWith(XConstant.FILE_SCHEME)) { File file = new File(filePath.substring(XConstant.FILE_SCHEME.length())); if (file.exists()) { return true; } } return false; }
From source file:org.zywx.wbpalmstar.engine.EUtil.java
public static String copyFileToStorage(Context context, String inFilePath) { if (!BUtility.sdCardIsWork()) { return null; }/* w w w . j a va2 s . c om*/ String ext = Environment.getExternalStorageDirectory() + File.separator + "download"; String fileName = subFileName(inFilePath); String newFilePath = ext + File.separator + fileName; File file = new File(newFilePath); if (file.exists()) { return newFilePath; } try { AssetManager assrt = context.getAssets(); InputStream input = assrt.open(inFilePath); file.createNewFile(); FileOutputStream output = new FileOutputStream(file); byte[] temp = new byte[8 * 1024]; int i = 0; while ((i = input.read(temp)) > 0) { output.write(temp, 0, i); } output.close(); input.close(); } catch (Exception e) { return null; } return newFilePath; }
From source file:com.dream.library.utils.AbFileUtil.java
/** * ???Asset?./*from w w w .j a v a 2s .co m*/ * * @param context the mContext * @param fileName the file name * @return Bitmap */ public static Bitmap getBitmapFromAsset(Context context, String fileName) { Bitmap bit = null; try { AssetManager assetManager = context.getAssets(); InputStream is = assetManager.open(fileName); bit = BitmapFactory.decodeStream(is); } catch (Exception e) { AbLog.e("?" + e.getMessage()); } return bit; }
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; }/* w w w.j a v a 2 s .c o m*/ 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:com.dream.library.utils.AbFileUtil.java
/** * ???Asset?.//w w w.j a va2s .c o m * * @param context the mContext * @param fileName the file name * @return Drawable */ public static Drawable getDrawableFromAsset(Context context, String fileName) { Drawable drawable = null; try { AssetManager assetManager = context.getAssets(); InputStream is = assetManager.open(fileName); drawable = Drawable.createFromStream(is, null); } catch (Exception e) { AbLog.e("?" + e.getMessage()); } return drawable; }