Example usage for android.database Cursor moveToFirst

List of usage examples for android.database Cursor moveToFirst

Introduction

In this page you can find the example usage for android.database Cursor moveToFirst.

Prototype

boolean moveToFirst();

Source Link

Document

Move the cursor to the first row.

Usage

From source file:Main.java

public static int getStatus(Context context) {
    Cursor query = null;
    String packageName = context.getPackageName();
    try {/*  www.j  ava  2s  . co  m*/
        query = context.getContentResolver().query(Uri.parse("content://com.lbe.security.miui.permmgr/active"),
                null, "pkgName=?", new String[] { packageName }, null);
        if (query == null) {
            return 0;
        }
        if (query.moveToFirst()) {
            int status = query.getInt(query.getColumnIndex("userAccept"));
            if (query == null) {
                return status;
            }
        }
        query.close();
    } catch (Exception e) {

        return -1;
    }

    return -1;
}

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 The context.//from w  w  w.j a v a2s.  com
 * @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);
        }
    } catch (Exception e) {
        return null;
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

public static String uriToFilePath(Context context, String contentUri) {
    if (Uri.parse(contentUri).getScheme().equals("content")) {
        String[] p = { MediaStore.MediaColumns.DATA };
        Cursor cursor = context.getContentResolver().query(Uri.parse(contentUri), p, // which columns
                null, // which rows (all rows)
                null, // selection args (none)
                null); // order-by clause (ascending by name)
        if (cursor != null) {
            int iColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            if (cursor.moveToFirst()) {
                return (cursor.getString(iColumn));
            }/*from   w  ww .  j ava2  s.  co  m*/
        }
    }
    if (Uri.parse(contentUri).getScheme().equals("file")) {
        return (Uri.parse(contentUri).getPath());
    }
    return (null);
}

From source file:info.guardianproject.otr.app.im.app.DatabaseUtils.java

public static RoundedAvatarDrawable getAvatarFromAddress(ContentResolver cr, String address, int width,
        int height) throws DecoderException {

    String[] projection = { Imps.Contacts.AVATAR_DATA };
    String[] args = { address };//www . java 2 s  . c o m
    String query = Imps.Contacts.USERNAME + " LIKE ?";
    Cursor cursor = cr.query(Imps.Contacts.CONTENT_URI, projection, query, args,
            Imps.Contacts.DEFAULT_SORT_ORDER);

    if (cursor.moveToFirst()) {
        String hexData = cursor.getString(0);
        cursor.close();
        if (hexData.equals("NULL")) {
            return null;
        }

        byte[] data = Hex.decodeHex(hexData.substring(2, hexData.length() - 1).toCharArray());

        return decodeAvatar(data, width, height);
    } else {

        cursor.close();
        return null;
    }
}

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

/**
 * Method that returns the album thumbnail path by its identifier.
 *
 * @param cr The ContentResolver//from ww  w  .jav  a  2 s .  c  om
 * @param albumId The album identifier to search
 * @return String The album thumbnail path
 */
public static String getAlbumThumbnailPath(@NonNull final ContentResolver cr, final long albumId) {
    final String[] projection = { MediaStore.Audio.Albums.ALBUM_ART };
    final String where = BaseColumns._ID + "=?";
    Cursor c = cr.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, projection, where,
            new String[] { String.valueOf(albumId) }, null);
    try {
        if (c != null && c.moveToFirst()) {
            return c.getString(0);
        }
    } finally {
        IOUtils.closeQuietly(c);
    }
    return null;
}

From source file:sg.macbuntu.android.pushcontacts.SmsReceiver.java

private static String getNameFromPhoneNumber(Context context, String phone) {
    Cursor cursor = context.getContentResolver().query(
            Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, phone),
            new String[] { Contacts.Phones.NAME }, null, null, null);
    if (cursor != null) {
        try {//from   w ww  . j  a  v  a2  s.  com
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                String name = cursor.getString(0);
                Log.e("PUSH_CONTACTS", "Pushed name: " + name);
                return name;
            }
        } finally {
            cursor.close();
        }
    }
    return null;
}

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

/**
 * Method that returns an array with all the unique albums paths and ids.
 *
 * @param cr The ContentResolver/* w  w w .j ava2 s  .  co  m*/
 * @return The albums map
 */
public static Map<String, Long> getAllAlbums(ContentResolver cr) {
    final Map<String, Long> albums = new HashMap<>();
    final String[] projection = { "distinct " + MediaStore.Audio.Media.ALBUM_ID,
            "substr(" + MediaStore.Audio.Media.DATA + ", 0, length(" + MediaStore.Audio.Media.DATA
                    + ") - length(" + MediaStore.Audio.Media.DISPLAY_NAME + "))" };
    final String where = MediaStore.Audio.Media.IS_MUSIC + " = ?";
    Cursor c = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, where, new String[] { "1" },
            null);
    if (c != null) {
        try {
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                long albumId = c.getLong(0);
                String albumPath = c.getString(1);
                albums.put(albumPath, albumId);
            }
        } finally {
            IOUtils.closeQuietly(c);
        }
    }
    return albums;
}

From source file:net.ccghe.emocha.model.DBAdapter.java

private static String getFirst(String column, String filter, String table, int columnId) {
    String result = null;//w  w  w .j  a va 2  s  .co m
    Cursor c = sDB.query(table, new String[] { column }, filter, null, null, null, null);
    if (c.getCount() > 0) {
        c.moveToFirst();
        result = c.getString(columnId);
    }
    c.close();
    return result;
}

From source file:Main.java

public static String getRealPathFromURI(Context context, long album_id) {
    Cursor cursor = null;
    try {//from   www.j a  v  a2 s  . com
        cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Audio.Albums.ALBUM_ART }, MediaStore.Audio.Albums._ID + "=?",
                new String[] { String.valueOf(album_id) }, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

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 va  2 s . com*/
 *            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;
}