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

private static InputStream openPhoto(final Context context, final Uri contactUri) {
    Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { ContactsContract.Contacts.Photo.PHOTO }, null, null, null);
    if (cursor == null) {
        return null;
    }//w w w .ja  va2 s . c om
    try {
        if (cursor.moveToFirst()) {
            byte[] data = cursor.getBlob(0);
            if (data != null) {
                return new ByteArrayInputStream(data);
            }
        }
    } finally {
        cursor.close();
    }
    return null;
}

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 www  . j  av  a 2  s . c  o 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:net.sf.sprockets.database.Cursors.java

/**
 * Get all long values in the first column.
 *
 * @param close true to close the cursor or false to leave it open
 *///from  www  .jav  a2  s  . c  o m
public static long[] allLongs(Cursor cursor, boolean close) {
    long[] l = EMPTY_LONG_ARRAY;
    if (cursor.moveToFirst()) {
        l = new long[cursor.getCount()];
        do {
            l[cursor.getPosition()] = cursor.getLong(0);
        } while (cursor.moveToNext());
    }
    close(cursor, close);
    return l;
}

From source file:Main.java

/**
 * //from   w  w  w . jav a 2s  .  c  om
 * @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;
}

From source file:Main.java

private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };
    try {/*from w ww  . j  a v a  2  s.c  o m*/
        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:net.sf.sprockets.database.Cursors.java

/**
 * Get all String values in the first column.
 *
 * @param close true to close the cursor or false to leave it open
 *//* ww  w  . j  a  v a  2s.  c om*/
public static String[] allStrings(Cursor cursor, boolean close) {
    String[] s = EMPTY_STRING_ARRAY;
    if (cursor.moveToFirst()) {
        s = new String[cursor.getCount()];
        do {
            s[cursor.getPosition()] = cursor.getString(0);
        } while (cursor.moveToNext());
    }
    close(cursor, close);
    return s;
}

From source file:Main.java

static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };
    try {//  ww  w.j  a v a 2 s .c  o  m
        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:edu.stanford.mobisocial.dungbeetle.social.FriendRequest.java

/**
 * Returns the contact id for the contact associated with the given uri,
 * or -1 if no such contact exists./*  w  w w.j  av a2  s.co  m*/
 */
public static long getExistingContactId(Context context, Uri friendRequest) {
    String personId = null;
    try {
        String pubKeyStr = friendRequest.getQueryParameter("publicKey");
        PublicKey key = RSACrypto.publicKeyFromString(pubKeyStr);
        personId = Util.makePersonIdForPublicKey(key);
    } catch (Exception e) {
        return -1;
    }
    Uri uri = Uri.parse(DungBeetleContentProvider.CONTENT_URI + "/contacts");
    String[] projection = new String[] { Contact._ID };
    String selection = Contact.PERSON_ID + " = ?";
    String[] selectionArgs = new String[] { personId };
    String sortOrder = null;
    Cursor c = context.getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
    if (!c.moveToFirst()) {
        return -1;
    }
    return c.getLong(0);
}

From source file:Main.java

/**
 * Get the contact name from a URI.//from www.j ava  2s .  c o m
 *
 * @param context The context.
 * @param contactUri The contact URI.
 *
 * @return The contact name.
 */
public static String getContactName(final Context context, Uri contactUri) {
    String name = null;
    if (contactUri != null) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(contactUri,
                    new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }

        }
    }

    return name;
}

From source file:Main.java

public static File uri2File(Activity context, Uri uri) {
    File file;/*  w ww .  j  av  a2 s .c  om*/
    String[] project = { MediaStore.Images.Media.DATA };
    Cursor actualImageCursor = context.getContentResolver().query(uri, project, null, null, null);
    if (actualImageCursor != null) {
        int actual_image_column_index = actualImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualImageCursor.moveToFirst();
        String img_path = actualImageCursor.getString(actual_image_column_index);
        file = new File(img_path);
    } else {
        file = new File(uri.getPath());
    }
    if (actualImageCursor != null)
        actualImageCursor.close();
    return file;
}