Example usage for android.content Context getContentResolver

List of usage examples for android.content Context getContentResolver

Introduction

In this page you can find the example usage for android.content Context getContentResolver.

Prototype

public abstract ContentResolver getContentResolver();

Source Link

Document

Return a ContentResolver instance for your application's package.

Usage

From source file:Main.java

public static String getDeviceId(Context context) {

    if (deviceId == null) {

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        String tmDevice, tmSerial, tmPhone, androidId;
        tmDevice = "" + tm.getDeviceId();
        tmSerial = "" + tm.getSimSerialNumber();
        androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(),
                android.provider.Settings.Secure.ANDROID_ID);

        UUID deviceUuid = new UUID(androidId.hashCode(),
                ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
        deviceId = deviceUuid.toString();

        System.err.println(deviceId);
    }/*  w  ww . ja va2s.c  o m*/
    return deviceId;
}

From source file:Main.java

public static void singleBroadcast(Context context, String filepath) {
    final int DIR_FORMAT = 0x3001; // directory
    Uri MediaUri = MediaStore.Files.getContentUri("external");
    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, filepath);
    values.put("format", DIR_FORMAT);
    values.put(MediaStore.MediaColumns.DATE_MODIFIED, System.currentTimeMillis() / 1000);
    context.getContentResolver().insert(MediaUri, values);
}

From source file:hobby.wei.c.phone.Network.java

@SuppressWarnings("deprecation")
public static void setAirplaneMode(Context context, boolean on) {
    Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, on ? 1 : 0);
}

From source file:Main.java

/**
 * Handles V19 and up uri's/* w  w w. j a va2s  . c o  m*/
 * @param context
 * @param contentUri
 * @return path
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getPathForV19AndUp(Context context, Uri contentUri) {
    String wholeID = DocumentsContract.getDocumentId(contentUri);

    // Split at colon, use second item in the array
    String id = wholeID.split(":")[1];
    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String sel = MediaStore.Images.Media._ID + "=?";
    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            sel, new String[] { id }, null);

    String filePath = "";
    int columnIndex = cursor.getColumnIndex(column[0]);
    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }

    cursor.close();
    return filePath;
}

From source file:Main.java

public static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) {
    // NOTE: There is in fact a 1 pixel border on the right side in the
    // ImageView/*from  ww  w.ja  v a2  s  .c  o m*/
    // used to display this drawable. Take it into account now, so we don't
    // have to
    // scale later.
    w -= 1;
    ContentResolver res = context.getContentResolver();
    Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
    if (uri != null) {
        ParcelFileDescriptor fd = null;
        try {
            fd = res.openFileDescriptor(uri, "r");
            int sampleSize = 1;

            // Compute the closest power-of-two scale factor
            // and pass that to sBitmapOptionsCache.inSampleSize, which will
            // result in faster decoding and better quality
            sBitmapOptionsCache.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);
            int nextWidth = sBitmapOptionsCache.outWidth >> 1;
            int nextHeight = sBitmapOptionsCache.outHeight >> 1;
            while (nextWidth > w && nextHeight > h) {
                sampleSize <<= 1;
                nextWidth >>= 1;
                nextHeight >>= 1;
            }

            sBitmapOptionsCache.inSampleSize = sampleSize;
            sBitmapOptionsCache.inJustDecodeBounds = false;
            Bitmap b = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);

            if (b != null) {
                // finally rescale to exactly the size we need
                if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
                    Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
                    // Bitmap.createScaledBitmap() can return the same
                    // bitmap
                    if (tmp != b)
                        b.recycle();
                    b = tmp;
                }
            }

            return b;
        } catch (FileNotFoundException e) {
        } finally {
            try {
                if (fd != null)
                    fd.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}

From source file:com.csipsimple.backup.SipProfileJson.java

public static void restoreSipAccounts(Context ctxt, JSONArray accounts) {
    ContentResolver cr = ctxt.getContentResolver();
    // Clear old existing accounts
    cr.delete(SipProfile.ACCOUNT_URI, "1", null);
    cr.delete(SipManager.FILTER_URI, "1", null);

    // Add each accounts
    for (int i = 0; i < accounts.length(); i++) {
        try {//from w  w w .  ja va2  s . c  o m
            JSONObject account = accounts.getJSONObject(i);
            restoreSipProfile(account, cr);
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Unable to parse item " + i, e);
        }
    }
}

From source file:Main.java

/**
 * Get {@link File} object for the given Android URI.<br>
 * Use content resolver to get real path if direct path doesn't return valid file.
 *//*  www.  jav a2  s .  c  o  m*/
private static File getFileFromUri(Context context, Uri uri) {

    // first try by direct path
    File file = new File(uri.getPath());
    if (file.exists()) {
        return file;
    }

    // try reading real path from content resolver (gallery images)
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String realPath = cursor.getString(column_index);
            file = new File(realPath);
        }
    } catch (Exception ignored) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return file;
}

From source file:Main.java

public static boolean setScreenBrightness(Context context, int screenBrightness) {
    int brightness = screenBrightness;
    if (screenBrightness < 1) {
        brightness = 1;/*w  ww.j a v a 2  s .  co  m*/
    } else if (screenBrightness > 255) {
        brightness = screenBrightness % 255;
        if (brightness == 0) {
            brightness = 255;
        }
    }
    boolean result = Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,
            brightness);
    return result;
}

From source file:Main.java

/**
 * Reads a Bitmap from an Uri./*from   w ww  .ja  v a2s .co  m*/
 *
 * @param context
 * @param selectedImage
 * @return Bitmap
 */
public static Bitmap readBitmap(Context context, Uri selectedImage) {
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inScaled = false;
    //      options.inSampleSize = 3;
    AssetFileDescriptor fileDescriptor = null;
    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        try {
            bm = BitmapFactory.decodeFileDescriptor(
                    fileDescriptor != null ? fileDescriptor.getFileDescriptor() : null, null, options);
            if (fileDescriptor != null) {
                fileDescriptor.close();
            }
        } catch (IOException e) {
            return null;
        }
    }
    return bm;
}

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//from   w  ww.j a v  a2  s.  c o m
 *            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.
 * @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()) {

            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}