List of usage examples for android.content Context getContentResolver
public abstract ContentResolver getContentResolver();
From source file:Main.java
/** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context.//w w w . j ava 2 s . c om * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. * @author paulburke */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { if (DEBUG) DatabaseUtils.dumpCursor(cursor); final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) cursor.close(); } return null; }
From source file:Main.java
public static Drawable get_scaled_drawable_from_uri_string_for_square_container(Context context, String uri, int max_size) { Drawable drawable = null;//ww w . ja v a 2 s . c o m Uri img_uri = Uri.parse(uri); if (uri != null) { try { InputStream inputStream = context.getContentResolver().openInputStream(img_uri); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); int sx = bitmap.getWidth(); int sy = bitmap.getHeight(); int fsx = max_size; int fsy = max_size; if (sy > sx) fsx = (int) ((float) max_size * ((float) sx / (float) sy)); else if (sx > sy) fsy = (int) ((float) max_size * ((float) sy / (float) sx)); Bitmap small_bitmap = Bitmap.createScaledBitmap(bitmap, fsx, fsy, false); bitmap.recycle(); drawable = new BitmapDrawable(context.getResources(), small_bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } return drawable; }
From source file:Main.java
public static String getDataColumn(final Context context, final Uri uri, final String selection, final String[] selectionArgs) { Cursor cursor = null;//ww w . j a va 2s. com final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) { cursor.close(); } } return null; }
From source file:com.example.facebook_photo.Utility.java
public static byte[] scaleImage(Context context, Uri photoUri) throws IOException { InputStream is = context.getContentResolver().openInputStream(photoUri); BitmapFactory.Options dbo = new BitmapFactory.Options(); dbo.inJustDecodeBounds = true;/*from w w w .j a va 2s .co m*/ BitmapFactory.decodeStream(is, null, dbo); is.close(); int rotatedWidth, rotatedHeight; int orientation = getOrientation(context, photoUri); if (orientation == 90 || orientation == 270) { rotatedWidth = dbo.outHeight; rotatedHeight = dbo.outWidth; } else { rotatedWidth = dbo.outWidth; rotatedHeight = dbo.outHeight; } Bitmap srcBitmap; is = context.getContentResolver().openInputStream(photoUri); if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) { float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION); float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION); float maxRatio = Math.max(widthRatio, heightRatio); // Create the bitmap from file BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = (int) maxRatio; srcBitmap = BitmapFactory.decodeStream(is, null, options); } else { srcBitmap = BitmapFactory.decodeStream(is); } is.close(); /* * if the orientation is not 0 (or -1, which means we don't know), we * have to do a rotation. */ if (orientation > 0) { Matrix matrix = new Matrix(); matrix.postRotate(orientation); srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true); } String type = context.getContentResolver().getType(photoUri); ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (type.equals("image/png")) { srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); } else if (type.equals("image/jpg") || type.equals("image/jpeg")) { srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); } byte[] bMapArray = baos.toByteArray(); baos.close(); return bMapArray; }
From source file:disono.webmons.com.utilities.helpers.WBFile.java
/** * Bitmap URI//from w ww. java 2 s . c om * * @param context * @param inImage * @return */ public static Uri getBmpUri(Context context, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "Title", null); return Uri.parse(path); }
From source file:Main.java
/** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * /* ww w .j a va 2s.c o m*/ * @param context * The context. * @param uri * The Uri to query. * @param selection * (Optional) Filter used in the query. * @param selectionArgs * (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = MediaStore.MediaColumns.DATA; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) cursor.close(); } return null; }
From source file:Main.java
public static boolean isAccessibilityServiceEnabled(Context context, Class<? extends AccessibilityService> accessibilityService) { ComponentName expectedComponentName = new ComponentName(context, accessibilityService); String enabledServicesSetting = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); if (enabledServicesSetting == null) return false; TextUtils.SimpleStringSplitter colonSplitter = new TextUtils.SimpleStringSplitter(':'); colonSplitter.setString(enabledServicesSetting); while (colonSplitter.hasNext()) { String componentNameString = colonSplitter.next(); ComponentName enabledService = ComponentName.unflattenFromString(componentNameString); if (enabledService != null && enabledService.equals(expectedComponentName)) return true; }// www. jav a 2 s . c o m return false; }
From source file:Main.java
public static Bitmap decodeSampledBitmapFromStream(Context context, Uri uri, int reqWidth, int reqHeight) throws IOException { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//from www . jav a2s . c o m InputStream stream = context.getContentResolver().openInputStream(uri); BitmapFactory.decodeStream(stream, null, options); stream.close(); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; stream = context.getContentResolver().openInputStream(uri); Bitmap bmp = BitmapFactory.decodeStream(stream, null, options); stream.close(); return bmp; }
From source file:com.textuality.lifesaver2.Columns.java
public static Map<String, Boolean> loadKeys(Context context, Uri provider, Columns columns) { Cursor cursor = context.getContentResolver().query(provider, null, null, null, null); Boolean exists = new Boolean(true); Map<String, Boolean> map = new Hashtable<String, Boolean>(); while (cursor.moveToNext()) { map.put(columns.cursorToKey(cursor), exists); }//from w w w . j a v a 2 s .c om cursor.close(); return map; }
From source file:Main.java
private final static void updateMeta(Context ctx, Uri uri, String name, long ts) { ContentValues cv = new ContentValues(); cv.put(MediaStore.Images.ImageColumns.DATE_TAKEN, ts); cv.put(MediaStore.Images.ImageColumns.IS_PRIVATE, 1); cv.put(MediaStore.Images.ImageColumns.TITLE, name); ctx.getContentResolver().update(uri, cv, null, null); }