Example usage for android.content ContentResolver query

List of usage examples for android.content ContentResolver query

Introduction

In this page you can find the example usage for android.content ContentResolver query.

Prototype

public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri, @Nullable String[] projection,
        @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) 

Source Link

Document

Query the given URI, returning a Cursor over the result set.

Usage

From source file:Main.java

public static Cursor getAllContacts(ContentResolver cr) {
    return cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
}

From source file:Main.java

public static void askForPermission(final Context context) {
    final ContentResolver cr = context.getContentResolver();
    cr.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
}

From source file:Main.java

public static final String getTelefone(final Context contexto, final String idContato) {
    ContentResolver cr = contexto.getContentResolver();
    Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + idContato, null, null);
    String numero = "";
    while (phones.moveToNext()) {
        numero = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        break;/*from ww  w. ja  v  a2s  . co m*/
    }
    phones.close();
    return numero;
}

From source file:Main.java

public static List<String> displayContacts(ContentResolver contentResolver) {

    List<String> contacts = new ArrayList<String>();
    ContentResolver cr = contentResolver;
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(
                    cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur
                            .getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                    contacts.add(name + " : " + phoneNo);
                }/* w w w .ja va  2  s .  com*/
                pCur.close();
            }
        }
    }
    return contacts;
}

From source file:Main.java

/**
 * Get path of image from uri//from w w w  . j a  va  2  s.  c o m
 *
 * @param contentResolver
 * @param contentURI
 * @return path of image. Null if not found.
 */
public static String getRealImagePathFromURI(ContentResolver contentResolver, Uri contentURI) {
    Cursor cursor = contentResolver.query(contentURI, null, null, null, null);
    if (cursor == null)
        return contentURI.getPath();
    else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(Images.ImageColumns.DATA);
        try {
            return cursor.getString(idx);
        } catch (Exception exception) {
            return null;
        }
    }
}

From source file:Main.java

/**
 * Get path of video from uri/* w w w  . j a v  a 2  s .  c  o m*/
 *
 * @param contentResolver
 * @param contentURI
 * @return path of video. Null if not found.
 */
public static String getRealVideoPathFromURI(ContentResolver contentResolver, Uri contentURI) {
    Cursor cursor = contentResolver.query(contentURI, null, null, null, null);
    if (cursor == null)
        return contentURI.getPath();
    else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(Video.VideoColumns.DATA);
        try {
            return cursor.getString(idx);
        } catch (Exception exception) {
            return null;
        }
    }
}

From source file:Main.java

public static boolean hasContact(Context mContext, String contact) {
    ContentResolver cr = mContext.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(
                    cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur
                            .getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    if (phoneNo.contains(contact) || contact.contains(phoneNo)) {
                        return true;
                    }/* w  ww  .j  ava2 s.com*/
                }
                pCur.close();
            }
        }
    }
    return false;
}

From source file:Main.java

public static void DeleteImageFromGalleryBase(final Context context, String fileName) {

    // Set up the projection (we only need the ID)
    String[] projection = { MediaStore.Images.Media._ID };

    // Match on the file path
    String selection = MediaStore.Images.Media.DATA + " = ?";
    String[] selectionArgs = new String[] { fileName };

    // Query for the ID of the media matching the file path
    Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    ContentResolver contentResolver = context.getContentResolver();
    Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
    if (c.moveToFirst()) {
        // We found the ID. Deleting the item via the content provider will also remove the file
        long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
        Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
        contentResolver.delete(deleteUri, null, null);
    } else {/*from   w  ww .  j a va2  s. c om*/
        // File not found in media store DB
    }
    c.close();
}

From source file:Main.java

static int getCardId(Context context) {
    ContentResolver res = context.getContentResolver();
    Cursor c = res.query(Uri.parse("content://media/external/fs_id"), null, null, null, null);
    int id = -1;//from www  .ja  v  a2 s.com
    if (c != null) {
        c.moveToFirst();
        id = c.getInt(0);
        c.close();
    }
    return id;
}

From source file:Main.java

/**
 * //from w  w w.ja  v a 2 s  .co m
 * @param context
 * @param uri
 *            uri of SCHEME_FILE or SCHEME_CONTENT
 * @return image path; uri will be changed to SCHEME_FILE
 */
public static String uriToImagePath(Context context, Uri uri) {
    if (context == null || uri == null) {
        return null;
    }

    String imagePath = null;
    String uriString = uri.toString();
    String uriSchema = uri.getScheme();
    if (uriSchema.equals(ContentResolver.SCHEME_FILE)) {
        imagePath = uriString.substring("file://".length());
    } else {// uriSchema.equals(ContentResolver.SCHEME_CONTENT)
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, null);
        if (cursor.getCount() == 0) {
            Log.e(TAG, "Uri(" + uri.toString() + ") not found!");
            return null;
        }
        cursor.moveToFirst();
        imagePath = cursor.getString(1);
        // Change the SCHEME_CONTENT uri to the SCHEME_FILE.
        uri = Uri.fromFile(new File(imagePath));
    }
    Log.v(TAG, "Final uri: " + uri.toString());
    return imagePath;
}