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 File getFromMediaUri(ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;
    if ("file".equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if ("content".equals(uri.getScheme())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;//from   ww w  .  j  ava  2  s  .co  m
        try {
            cursor = resolver.query(uri, filePathColumn, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int columnIndex = (uri.toString().startsWith("content://com.google.android.gallery3d"))
                        ? cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
                        : cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                if (columnIndex != -1) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (SecurityException ignored) {
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}

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

private static long lookupHighWatermark(ContentResolver resolver, long id) {
    long result = 0;
    final Cursor c = resolver.query(RawContacts.CONTENT_URI, HighWatermarkQuery.PROJECTION,
            HighWatermarkQuery.SELECTION, new String[] { Long.toString(id) }, null);
    try {//ww  w .  j  ava 2 s  . co m
        while (c.moveToNext()) {
            result = c.getLong(HighWatermarkQuery.COLUMN_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return result;
}

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

private static String lookupUsername(ContentResolver resolver, long id) {
    String result = null;//w ww .  j  a v  a 2s. c  o  m
    final Cursor c = resolver.query(RawContacts.CONTENT_URI, UsernameQuery.PROJECTION, UsernameQuery.SELECTION,
            new String[] { Long.toString(id) }, null);
    try {
        while (c.moveToNext()) {
            result = c.getString(UsernameQuery.COLUMN_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return result;
}

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

/**
 * Returns the RawContact id for a sample SyncAdapter contact, or 0 if the
 * sample SyncAdapter user isn't found./*from  w ww  .  j a va 2 s.c o m*/
 * 
 * @param context the Authenticator Activity context
 * @param userId the sample SyncAdapter user ID to lookup
 * @return the RawContact id, or 0 if not found
 */
private static long lookupRawContact(ContentResolver resolver, String userName) {
    long authorId = 0;
    final Cursor c = resolver.query(RawContacts.CONTENT_URI, UserIdQuery.PROJECTION, UserIdQuery.SELECTION,
            new String[] { userName }, null);
    try {
        if (c.moveToFirst()) {
            authorId = c.getLong(UserIdQuery.COLUMN_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return authorId;
}

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

/**
 * Returns the Data id for a sample SyncAdapter contact's profile row, or 0
 * if the sample SyncAdapter user isn't found.
 * /*from  ww w.j  av  a 2s  . co m*/
 * @param resolver a content resolver
 * @param userId the sample SyncAdapter user ID to lookup
 * @return the profile Data row id, or 0 if not found
 */
private static long lookupProfile(ContentResolver resolver, String userName) {
    long profileId = 0;
    final Cursor c = resolver.query(Data.CONTENT_URI, ProfileQuery.PROJECTION, ProfileQuery.SELECTION,
            new String[] { userName }, null);
    try {
        if (c != null && c.moveToFirst()) {
            profileId = c.getLong(ProfileQuery.COLUMN_ID);
            Log.d("ProfileLookup", Long.toString(c.getLong(ProfileQuery.COLUMN_ID)));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return profileId;
}

From source file:com.trk.aboutme.facebook.Settings.java

/**
 * Acquire the current attribution id from the facebook app.
 * @return returns null if the facebook app is not present on the phone.
 *//*from   w w w . ja  v  a  2  s  . com*/
public static String getAttributionId(ContentResolver contentResolver) {
    String[] projection = { ATTRIBUTION_ID_COLUMN_NAME };
    Cursor c = contentResolver.query(ATTRIBUTION_ID_CONTENT_URI, projection, null, null, null);
    if (c == null || !c.moveToFirst()) {
        return null;
    }
    String attributionId = c.getString(c.getColumnIndex(ATTRIBUTION_ID_COLUMN_NAME));
    c.close();
    return attributionId;
}

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

/**
 * Returns the RawContact id for a sample SyncAdapter contact, or 0 if the
 * sample SyncAdapter user isn't found.//from   w  ww  . j  a va  2  s  . c o m
 * 
 * @param context the Authenticator Activity context
 * @param userId the sample SyncAdapter user ID to lookup
 * @return the RawContact id, or 0 if not found
 */
private static List<Long> lookupAllContacts(ContentResolver resolver) {
    List<Long> result = new ArrayList<Long>();
    final Cursor c = resolver.query(RawContacts.CONTENT_URI, AllUsersQuery.PROJECTION, AllUsersQuery.SELECTION,
            null, null);
    try {
        while (c.moveToNext()) {
            result.add(c.getLong(AllUsersQuery.COLUMN_ID));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return result;
}

From source file:com.cyanogenmod.filemanager.util.MediaHelper.java

/**
 * Method that converts a content uri to a file system path
 *
 * @param cr The content resolver/*from   w  w  w.jav a 2 s. com*/
 * @param id The media database id
 * @param volume The volume
 * @return File The file reference
 */
private static File mediaIdToFile(ContentResolver cr, long id, String volume) {
    final String[] projection = { MediaColumns.DATA };
    final String where = MediaColumns._ID + " = ?";
    Uri baseUri = MediaStore.Files.getContentUri(volume);
    Cursor c = cr.query(baseUri, projection, where, new String[] { String.valueOf(id) }, null);
    try {
        if (c != null && c.moveToNext()) {
            return new File(c.getString(c.getColumnIndexOrThrow(MediaColumns.DATA)));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return null;
}

From source file:edu.mit.mobile.android.locast.data.TaggableItem.java

/**
 * @param cr//from  w w  w.  ja  va 2s  .  c o  m
 * @param item
 * @param prefix
 * @return a list of all the tags attached to a given item
 */
public static Set<String> getTags(ContentResolver cr, Uri item, String prefix) {
    final Cursor tags = cr.query(Uri.withAppendedPath(item, Tag.PATH), Tag.DEFAULT_PROJECTION, null, null,
            null);
    final Set<String> tagSet = new HashSet<String>(tags.getCount());
    final int tagColumn = tags.getColumnIndex(Tag._NAME);
    final Predicate<String> predicate = getPrefixPredicate(prefix);
    for (tags.moveToFirst(); !tags.isAfterLast(); tags.moveToNext()) {
        final String tag = tags.getString(tagColumn);
        if (predicate.apply(tag)) {
            final int separatorIndex = tag.indexOf(PREFIX_SEPARATOR);
            if (separatorIndex == -1) {
                tagSet.add(tag);
            } else {
                tagSet.add(tag.substring(separatorIndex + 1));
            }
        }
    }
    tags.close();
    return tagSet;
}

From source file:Main.java

public static File getFromMediaUri(ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;

    if (SCHEME_FILE.equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if (SCHEME_CONTENT.equals(uri.getScheme())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;/*from  w  w w  .  j  a va 2  s  . c  o m*/
        try {
            cursor = resolver.query(uri, filePathColumn, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int columnIndex = (uri.toString().startsWith("content://com.google.android.gallery3d"))
                        ? cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
                        : cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                // Picasa image on newer devices with Honeycomb and up
                if (columnIndex != -1) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (SecurityException ignored) {
            // Nothing we can do
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}