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 long selectTopTableId(SQLiteDatabase mDB, String tableName, String tableIdColumn) {
    long topTaskId = 1;
    Cursor cursor = mDB.query(tableName, new String[] { tableIdColumn }, null, null, null, null,
            tableIdColumn + " DESC LIMIT 1");
    assert null != cursor;
    try {//from  ww  w .j  a v a2 s. c  o m
        if (cursor.moveToFirst()) {
            topTaskId = cursor.getLong(0);
        }
    } finally {
        cursor.close();
    }
    return topTaskId;
}

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  w  w  .  j  a 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:Main.java

public static Uri getBitmapFromImageId(Activity activity, int imageId) {
    String[] tinyImgPprojection = { MediaStore.Images.Thumbnails._ID };
    Cursor tinyCursor = Thumbnails.queryMiniThumbnail(activity.getContentResolver(), imageId,
            Thumbnails.MINI_KIND, tinyImgPprojection);
    if (tinyCursor.getCount() != 0) {
        tinyCursor.moveToFirst();
        int tinyImgId = tinyCursor.getInt(tinyCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
        tinyCursor.close();/* w  ww  . j  av  a2 s . c o m*/
        return Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                String.valueOf(tinyImgId));
    } else {
        tinyCursor.close();
        return null;
    }
}

From source file:Main.java

public static Uri pathToContentUri(Context context, String imagePath) {
    Cursor cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[] { "_id" },
            "_data=? ", new String[] { imagePath }, null);
    if (cursor != null && cursor.moveToFirst()) {
        int imageFile1 = cursor.getInt(cursor.getColumnIndex("_id"));
        Uri values1 = Uri.parse("content://media/external/images/media");
        return Uri.withAppendedPath(values1, "" + imageFile1);
    } else {/*from  www . j  a va2  s.co  m*/
        File imageFile = new File(imagePath);
        if (imageFile.exists()) {
            ContentValues values = new ContentValues();
            values.put("_data", imagePath);
            Uri baseUri = Media.EXTERNAL_CONTENT_URI;
            return context.getContentResolver().insert(baseUri, values);
        } else {
            return null;
        }
    }
}

From source file:Main.java

public static String getRealFilePath(Context context, Uri uri) {
    if (null == uri)
        return null;
    String scheme = uri.getScheme();
    String data = null;/*  w  ww .ja va  2  s  .c o m*/
    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
                null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:Main.java

public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;//ww  w .j  av a2 s. c  o m

    try {
        Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        result = cursor.getString(column_index);

    } catch (Exception e) {
        result = null;
    }
    return result;
}

From source file:Main.java

/**
 * Get Image orientation from uri/*w  ww. j ava  2  s . com*/
 *
 * @param context  Context of image
 * @param photoUri Uri of image
 * @return
 */
public static int getOrientation(Context context, Uri photoUri) {
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    try {
        if (cursor.moveToFirst()) {
            if (cursor.getInt(0) == ORIENTATION_270)
                return ExifInterface.ORIENTATION_ROTATE_270;
            else if (cursor.getInt(0) == ORIENTATION_180)
                return ExifInterface.ORIENTATION_ROTATE_180;
            else if (cursor.getInt(0) == ORIENTATION_90)
                return ExifInterface.ORIENTATION_ROTATE_90;
        }
    } finally {
        cursor.close();
    }
    return -1;
}

From source file:Main.java

public static void deleteSMS(Context context, String message, String number) {
    try {/*from w  w w  . jav  a 2  s.c  om*/

        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(uriSms,
                new String[] { "_id", "thread_id", "address", "person", "date", "body" }, null, null, null);

        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                long threadId = c.getLong(1);
                String address = c.getString(2);
                String body = c.getString(5);

                if (message.equals(body) && address.equals(number)) {

                    context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        // mLogger.logError("Could not delete SMS from inbox: " + e.getMessage());
    }
}

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  .j a va 2  s . c  o 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 "";
}

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

protected static boolean isRowExisting(Uri uri, String[] projection, ContentResolver resolver) {
    final Cursor cursor = resolver.query(uri, projection, null, null, null);
    try {//from  w  ww  .  ja  va  2 s .  co  m
        if (!cursor.moveToFirst())
            return false;
    } finally {
        cursor.close();
    }
    return true;
}