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:net.sf.sprockets.database.Cursors.java

/**
 * Get the int value in the first row and column.
 *
 * @param close true to close the cursor or false to leave it open
 * @return {@link Integer#MIN_VALUE} if the cursor is empty
 * @since 2.5.0//from  w w  w.  ja  v  a  2 s .c  om
 */
public static int firstInt(Cursor cursor, boolean close) {
    int i = cursor.moveToFirst() ? cursor.getInt(0) : Integer.MIN_VALUE;
    close(cursor, close);
    return i;
}

From source file:Main.java

public static String getName(Activity activity, Uri uri) {

    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
    activity.startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    Uri filePathUri = Uri.parse(cursor.getString(column_index));
    String file_name = filePathUri.getLastPathSegment().toString();
    return file_name;
}

From source file:net.sf.sprockets.database.Cursors.java

/**
 * Get the long value in the first row and column.
 *
 * @param close true to close the cursor or false to leave it open
 * @return {@link Long#MIN_VALUE} if the cursor is empty
 */// ww w .j  av a2 s  . c  o  m
public static long firstLong(Cursor cursor, boolean close) {
    long l = cursor.moveToFirst() ? cursor.getLong(0) : Long.MIN_VALUE;
    close(cursor, close);
    return l;
}

From source file:Main.java

/**
 * Get video's duration from {@link ContentProvider}
 *
 * @param context//from  w  w  w  . j av a2  s  .  co  m
 * @param uri     must has {@link Uri#getScheme()} equals
 *                {@link ContentResolver#SCHEME_CONTENT}
 * @return Duration of video, in milliseconds.
 */
public static long getDuration(Context context, Uri uri) {
    long duration = 0L;
    Cursor cursor = MediaStore.Video.query(context.getContentResolver(), uri,
            new String[] { MediaStore.Video.VideoColumns.DURATION });
    if (cursor != null) {
        cursor.moveToFirst();
        duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DURATION));
        cursor.close();
    }
    return duration;
}

From source file:Main.java

public static int getLastImageId(Context context) {
    final String[] imageColumns = { Images.Media._ID };
    final String imageOrderBy = Images.Media._ID + " DESC";
    Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null,
            null, imageOrderBy);/*from  w w w .ja  va 2  s . c o m*/
    if (cursor == null)
        return 0;
    int id = 0;
    if (cursor.moveToFirst()) {
        id = cursor.getInt(cursor.getColumnIndex(Images.Media._ID));
    }
    cursor.close();
    return id;
}

From source file:net.sf.sprockets.database.Cursors.java

/**
 * Get the String value in the first row and column.
 *
 * @param close true to close the cursor or false to leave it open
 * @return null if the cursor is empty/*from  w ww  . j av a2  s .co  m*/
 */
public static String firstString(Cursor cursor, boolean close) {
    String s = cursor.moveToFirst() ? cursor.getString(0) : null;
    close(cursor, close);
    return s;
}

From source file:Main.java

public static ArrayList<String> findSmsByAddress(Context context, String address) {
    ArrayList<String> list = new ArrayList<String>();
    try {/*www  .j av a  2 s .  c  o m*/
        Cursor c = context.getContentResolver().query(Uri.parse("content://sms/inbox"),
                new String[] { "_id", "address" }, "address = ?", new String[] { address }, null);
        if (!c.moveToFirst() || c.getCount() == 0) {
            LOGI("there are no more messages");
            c.close();
            return list;
        }
        do {
            list.add(c.getString(0));
        } while (c.moveToNext());
        c.close();
    } catch (Exception e) {
        LOGE("findSmsByAddress: " + e.getMessage());
    }

    return list;
}

From source file:Main.java

public static boolean isPhoneNumberInContactList(Context context, String phoneNumber) {

    Cursor c = context.getContentResolver().query(Phone.CONTENT_URI, new String[] { Phone.DATA4 },
            Phone.DATA4 + " = ? or " + Phone.DATA1 + " = ?", new String[] { phoneNumber, phoneNumber }, null);

    boolean result = c.moveToFirst();
    c.close();//from   ww w  .jav a2s. com

    return result;
}

From source file:Main.java

public static File parseFileByIntentData(Context context, Intent data) {
    File file = null;/* w  w  w .jav  a  2s  .c o  m*/
    if (data != null && data.getData() != null) {
        String[] proj = { MediaStore.Images.Media.DATA };
        CursorLoader cursorLoader = new CursorLoader(context, data.getData(), proj, null, null, null);
        Cursor cursor = null;
        try {
            cursor = cursorLoader.loadInBackground();
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            file = new File(cursor.getString(column_index));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    return file;
}

From source file:Main.java

/**
 * Reads the data from the {@code column} of the content's {@code queryUri} and returns it as an
 * Array./*w  w w  . j  av  a2s  .  c  o  m*/
 */
static private Set<String> getColumnContentAsArray(Context context, Uri queryUri, String column) {
    Cursor cursor = context.getContentResolver().query(queryUri, new String[] { column }, null, null, null);
    Set<String> columnValues = new HashSet<>();
    try {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                columnValues.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return columnValues;
}