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 HashSet<String> getUserContacts(ContentResolver cr) {
    HashSet<String> contactsList = new HashSet<String>();
    //ContentResolver cr = 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));
            // We could store names and numbers in a dictionary if we ever needed that
            // 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 the number stored isn't long enough to be in our database then don't bother.
                    if (phoneNo.length() >= 10) {
                        // Use regex to remove '+', spaces, '(', and ')' and '-' form user contacts to match database format
                        String formattedPhoneNo = phoneNo.replaceAll(DELIMITERS, "");
                        // Remove the '1' in front of the number
                        if (formattedPhoneNo.charAt(0) == '1') {
                            formattedPhoneNo = formattedPhoneNo.substring(1);
                        }/*from   ww  w  .j a  va  2s .c om*/
                        contactsList.add(formattedPhoneNo);
                    }
                }
                pCur.close();
            }
        }
    }
    return contactsList;
}

From source file:Main.java

private static int getImageRotationAngle(Uri imageUri, ContentResolver contentResolver) throws IOException {
    int angle = 0;
    Cursor cursor = contentResolver.query(imageUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
            null, null, null);/*from w w w.  j  a v  a 2 s .  c  o  m*/
    if (cursor != null) {
        if (cursor.getCount() == 1) {
            cursor.moveToFirst();
            angle = cursor.getInt(0);
        }
        cursor.close();
    } else {
        ExifInterface exif = new ExifInterface(imageUri.getPath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            angle = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            angle = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            angle = 90;
            break;
        default:
            break;
        }
    }
    return angle;
}

From source file:Main.java

public static String getSdCardPath(ContentResolver cr, Uri uri, String defaultPath) {
    ArrayList files = new ArrayList();
    if (uri != null) {
        Cursor mCursor = cr.query(uri, null, null, null, null);
        if (mCursor != null) {
            while (mCursor.moveToNext()) {
                String path = mCursor.getString(mCursor.getColumnIndexOrThrow("_data"));
                if (!path.contains(defaultPath)) {
                    String parentFolder = (new File(path)).getParent();
                    if (!files.contains(parentFolder)) {
                        files.add(parentFolder);
                    }//  ww w.  ja  v  a2s  .  c o m
                }
            }
        }
    }

    return getCommonSubStringFromList(files);
}

From source file:com.goliathonline.android.kegbot.io.RemoteUserHandler.java

private static ContentValues queryUserDetails(Uri uri, ContentResolver resolver) {
    final ContentValues values = new ContentValues();
    final Cursor cursor = resolver.query(uri, UserQuery.PROJECTION, null, null, null);
    try {/* w  ww .ja  v a  2 s.c  om*/
        if (cursor.moveToFirst()) {
            values.put(SyncColumns.UPDATED, cursor.getLong(UserQuery.UPDATED));
        } else {
            values.put(SyncColumns.UPDATED, KegbotContract.UPDATED_NEVER);
        }
    } finally {
        cursor.close();
    }
    return values;
}

From source file:fr.mixit.android.io.JsonHandlerApplyInterests.java

private static boolean isItemUpdated(Uri uri, JSONObject item, ContentResolver resolver) throws JSONException {
    final Cursor cursor = resolver.query(uri, MixItContract.Interests.PROJ.PROJECTION, null, null, null);
    try {/*from ww w  . j  av  a 2  s .  co  m*/
        if (!cursor.moveToFirst()) {
            return false;
        }

        final String curName = cursor.getString(MixItContract.Interests.PROJ.NAME)
                .toLowerCase(Locale.getDefault()).trim();

        final String newName = item.has(TAG_NAME)
                ? item.getString(TAG_NAME).toLowerCase(Locale.getDefault()).trim()
                : curName;

        return !curName.equals(newName);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

/**
 * Gets the Uri to a specific audio file
 * @param filePath The path of the file that we are looking up
 * @param contentResolver The content resolver that is used to perform the query
 * @return The Uri of the sound file// ww  w.  j  av a 2s.  c om
 */
private static Uri getAudioUriFromFilePath(String filePath, ContentResolver contentResolver) {
    long audioId;
    Uri uri = MediaStore.Audio.Media.getContentUri("external");
    String[] projection = { BaseColumns._ID };
    Cursor cursor = contentResolver.query(uri, projection, MediaStore.MediaColumns.DATA + " LIKE ?",
            new String[] { filePath }, null);

    if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(projection[0]);
        audioId = cursor.getLong(columnIndex);
        cursor.close();
        return Uri.parse(uri.toString() + "/" + audioId);
    }
    return null;
}

From source file:com.deliciousdroid.platform.BundleManager.java

public static void UpsertBundle(Bundle bundle, String account, Context context) {
    String[] projection = new String[] { Bundle.Name, Bundle.Tags };
    String selection = Bundle.Name + "=? AND " + Bundle.Account + "=?";
    String[] selectionargs = new String[] { bundle.getName(), account };

    ContentResolver cr = context.getContentResolver();
    Cursor c = cr.query(Bundle.CONTENT_URI, projection, selection, selectionargs, null);

    if (c.moveToFirst()) {
        UpdateBundle(bundle, account, context);
    } else {//from   ww  w  .  jav a2s  . co m
        AddBundle(bundle, account, context);
    }
    c.close();
}

From source file:Main.java

public static Cursor getAllCallLogs(ContentResolver cr) {
    // reading all data in descending order according to DATE
    String strOrder = CallLog.Calls.DATE + " DESC";
    Uri callUri = Uri.parse("content://call_log/calls");
    Cursor curCallLogs = cr.query(callUri, null, null, null, strOrder);

    return curCallLogs;
}

From source file:Main.java

public static Cursor execute(ContentResolver cr, String select, String[] args, String[] _projection) {
    Uri callUri = Uri.parse(contentUri);
    if (_projection == null) {
        _projection = projection;/*  w  w  w  . j a  v a2  s  . com*/
    }

    return cr.query(callUri, _projection, select, args, logOrder);
}

From source file:Main.java

/**
 * Method to resolve the display name of a content URI.
 *
 * @param uri the content URI to be resolved.
 * @param contentResolver the content resolver to query.
 * @param columnField the column field to query.
 * @return the display name of the @code uri if present in the database
 *  or an empty string otherwise.//ww w .ja v  a2 s  . co  m
 */
public static String getDisplayName(Uri uri, ContentResolver contentResolver, String columnField) {
    if (contentResolver == null || uri == null)
        return "";
    Cursor cursor = null;
    try {
        cursor = contentResolver.query(uri, null, null, null, null);

        if (cursor != null && cursor.getCount() >= 1) {
            cursor.moveToFirst();
            int index = cursor.getColumnIndex(columnField);
            if (index > -1)
                return cursor.getString(index);
        }
    } catch (NullPointerException e) {
        // Some android models don't handle the provider call correctly.
        // see crbug.com/345393
        return "";
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return "";
}