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 Uri getUriFromPath(Context context, String path) {

    String fileName = "file:///sdcard/DCIM/Camera/2013_07_07_12345.jpg";
    Uri fileUri = Uri.parse(fileName);// www.j a  v a2s .com
    String filePath = fileUri.getPath();
    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
            "_data = '" + filePath + "'", null, null);
    cursor.moveToNext();
    int id = cursor.getInt(cursor.getColumnIndex("_id"));
    Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);

    return uri;
}

From source file:Main.java

public static String getImagePath(Context context, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(context.getContentResolver(), inImage, "Title", null);
    return path;//w  w  w .ja  v a 2s  . c o m
}

From source file:Main.java

private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;/*from www .j a v  a2  s .  c  o m*/
    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:Main.java

static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;/*from  ww w .j a  v  a 2  s .c o  m*/
    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 "";
}

From source file:Main.java

/**
 * retrieve the contact photo given a contact id
 *
 * @param context//w  w w .  jav a 2  s . c o m
 *            a context object used to get a content resolver
 * @param id
 *            the id number of contact
 *
 * @return the bitmap of the photo or null
 */
public static Bitmap loadContactPhoto(Context context, long id) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);

    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
            uri);
    if (input == null) {
        return null;
    }
    return BitmapFactory.decodeStream(input);
}

From source file:Main.java

public static int getContactIdFromPhoneNumber(final Context context, final String number) {
    final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    final String[] projection = { PhoneLookup._ID };
    final Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
    if (c.getCount() > 0) {
        c.moveToFirst();/*from w  w w. ja  va  2  s.com*/
        return c.getInt(0);
    } else {
        return -1;
    }
}

From source file:Main.java

public static boolean isLocationEnabled(Context context) {
    int locationMode = 0;
    String locationProviders;//  ww w  .  j  a va2  s .  co  m

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        try {
            locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);

        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return locationMode != Settings.Secure.LOCATION_MODE_OFF;
    } else {
        locationProviders = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        return !TextUtils.isEmpty(locationProviders);
    }
}

From source file:Main.java

/**
 * Gets a contact number and then displays the name of contact in the DP
 *
 * @param context       context of the activity from which it was called.
 * @param contactNumber a string representation of the contact number
 * @return returns the number if no contact exist.
 */// w ww  .j  a v  a 2s. c o m
public static String getContactName(Context context, String contactNumber) {
    String displayName = null;
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactNumber));

    Cursor cur = context.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

    if (cur != null && cur.moveToFirst()) {
        displayName = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    } else {
        displayName = contactNumber;
    }

    if (!cur.isClosed()) {
        cur.close();
    }

    return displayName;
}

From source file:Main.java

public static String getPath(Context context, Uri uri) {

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;/*from  w ww  .  j a v a2 s  .c o m*/

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {

        }
    }

    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

/**
 * Check if the IME specified by the context is the current IME.
 * CAVEAT: This may cause a round trip IPC.
 *
 * @param context package context of the IME to be checked.
 * @param imm the {@link InputMethodManager}.
 * @return true if this IME is the current IME.
 *///from w  w  w  . j  a v a2  s.  c  o m
public static boolean isThisImeCurrent(final Context context, final InputMethodManager imm) {
    final InputMethodInfo imi = getInputMethodInfoOf(context.getPackageName(), imm);
    final String currentImeId = Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.DEFAULT_INPUT_METHOD);
    return imi != null && imi.getId().equals(currentImeId);
}