List of usage examples for android.content Context getContentResolver
public abstract ContentResolver getContentResolver();
From source file:Main.java
/** @see http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue */ public static int getSampleSize(Context context, Uri uri, float maxSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;/*from w ww. j a v a2 s. c o m*/ if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) { try { InputStream stream = context.getContentResolver().openInputStream(uri); BitmapFactory.decodeStream(stream, null, options); stream.close(); } catch (Exception e) { e.printStackTrace(); } } else BitmapFactory.decodeFile(uri.getPath(), options); int longSide = Math.max(options.outHeight, options.outWidth); return getSampleSize(longSide, maxSize); }
From source file:com.dahl.brendan.wordsearch.view.IOService.java
private static void writeFile(Context context, File file, boolean overwrite) { if (!file.exists() || overwrite) { Cursor cursor = context.getContentResolver().query(Word.CONTENT_URI, new String[] { Word.WORD }, null, null, null);/* ww w .ja v a 2 s . c o m*/ JSONArray array = new JSONArray(); if (cursor.moveToFirst()) { while (!cursor.isAfterLast()) { array.put(cursor.getString(0)); cursor.moveToNext(); } } try { file.getParentFile().mkdirs(); file.createNewFile(); BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write(array.toString()); out.close(); } catch (IOException e) { Log.e(LOGTAG, "write failed", e); } } }
From source file:Main.java
/** * Adds the saved image to the gallery//from w w w.ja v a 2s . c o m * * From: http://stackoverflow.com/a/20859733 * @param filePath * @param context */ public static void addImageToGallery(final String filePath, final Context context) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); values.put(MediaStore.MediaColumns.DATA, filePath); context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); }
From source file:Main.java
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null;/*from w w w . j a v a 2 s. 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 index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; }
From source file:com.dahl.brendan.wordsearch.view.IOService.java
private static void readFile(Context context, File file, boolean overwrite) { if (file.canRead()) { if (overwrite) { context.getContentResolver().delete(Word.CONTENT_URI, "1", null); }//from w ww. j av a 2 s .c o m try { byte[] buffer = new byte[(int) file.length()]; BufferedInputStream f = new BufferedInputStream(new FileInputStream(file)); f.read(buffer); JSONArray array = new JSONArray(new String(buffer)); List<ContentValues> values = new LinkedList<ContentValues>(); for (int i = 0; i < array.length(); i++) { Log.v(LOGTAG, array.getString(i)); ContentValues value = new ContentValues(); value.put(Word.WORD, array.getString(i)); values.add(value); } context.getContentResolver().bulkInsert(Word.CONTENT_URI, values.toArray(new ContentValues[0])); } catch (IOException e) { Log.e(LOGTAG, "IO error", e); } catch (JSONException e) { Log.e(LOGTAG, "bad input", e); } } }
From source file:Main.java
public static boolean setMediaVolume(Context context, int mediaVloume) { if (mediaVloume < 0) { mediaVloume = 0;/*from ww w . ja va 2s . com*/ } else if (mediaVloume > 15) { mediaVloume = mediaVloume % 15; if (mediaVloume == 0) { mediaVloume = 15; } } return Settings.System.putInt(context.getContentResolver(), "volume_music", mediaVloume); // Settings.System.VOLUME_MUSIC, mediaVloume); }
From source file:com.amazonaws.utilities.Util.java
/** * Copies the data from the passed in Uri, to a new file for use with the * Transfer Service/* w ww .j a v a 2 s. c o m*/ * * @param context * @param uri * @return * @throws IOException */ public static File copyContentUriToFile(Context context, Uri uri) throws IOException { InputStream is = context.getContentResolver().openInputStream(uri); File copiedData = new File(context.getDir("SampleImagesDir", Context.MODE_PRIVATE), UUID.randomUUID().toString()); copiedData.createNewFile(); FileOutputStream fos = new FileOutputStream(copiedData); byte[] buf = new byte[2046]; int read = -1; while ((read = is.read(buf)) != -1) { fos.write(buf, 0, read); } fos.flush(); fos.close(); return copiedData; }
From source file:Main.java
/** * Query the server app to get the file's display name. *//* w w w.j a v a 2 s . c o m*/ public static String getUriFileName(Context context, Uri uri) { if (uri == null) { return null; } if (uri.getScheme().equals("file")) { return uri.getLastPathSegment(); } if (uri.getScheme().equals("content")) { Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null); //Get the column index of the data in the Cursor, //move to the first row in the Cursor, get the data, and display it. int name_index = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); //int size_index = returnCursor.getColumnIndex(OpenableColumns.SIZE); if (name_index < 0) { return null; } returnCursor.moveToFirst(); //return returnCursor.getLong(size_index) return returnCursor.getString(name_index); } return null; }
From source file:Main.java
/** * Returns the IMEI Number.// w ww . ja v a 2s . co m * @return - Device IMEI number. */ public static String getDeviceId(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String deviceId = telephonyManager.getDeviceId(); if (deviceId == null || deviceId.isEmpty()) { deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); } return deviceId; }
From source file:Main.java
/** * Get bitmap from a URI./* w w w . j av a 2 s .c om*/ * * @param context The context. * @param uriString The URI as a string. * * @return The bitmap. */ public static Bitmap getBitmapFromUri(final Context context, String uriString) { Bitmap bitmap = null; if (uriString == null) { return null; } Uri uri = Uri.parse(uriString); if (uri != null) { try { bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri); if (bitmap != null) { // We use default density for all bitmaps to avoid scaling. bitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT); } } catch (IOException e) { } } return bitmap; }