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

@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
    String filePath = "";
    String wholeID = DocumentsContract.getDocumentId(uri);

    // Split at colon, use second item in the array
    String id = wholeID.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String sel = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            sel, new String[] { id }, null);

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }/*from w ww .j  a va  2 s . c om*/
    cursor.close();
    return filePath;
}

From source file:co.rewen.statex.AsyncLocalStorageUtil.java

/**
 * Returns the value of the given key, or null if not found.
 *//*  w  ww .  jav  a2s .  c  o  m*/
/* package */
static @Nullable String getItemImpl(SQLiteDatabase db, String key) {
    String[] columns = { VALUE_COLUMN };
    String[] selectionArgs = { key };

    Cursor cursor = db.query(TABLE_STATE, columns, KEY_COLUMN + "=?", selectionArgs, null, null, null);

    try {
        if (!cursor.moveToFirst()) {
            return null;
        } else {
            return cursor.getString(0);
        }
    } finally {
        cursor.close();
    }
}

From source file:Main.java

/**
 * Get a uri's user-friendly display name
 * // ww w. jav  a2s . c  o m
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return a user-friendly display name
 */
public static String getUriDisplayName(Context context, Uri uri) {
    String displayName = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] proj = { OpenableColumns.DISPLAY_NAME };
        Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);

        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
            cursor.moveToFirst();
            displayName = cursor.getString(columnIndex);

            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        displayName = uri.getLastPathSegment();
    }

    return displayName;
}

From source file:net.peterkuterna.android.apps.devoxxsched.io.RemoteSpeakersHandler.java

private static boolean isSpeakerUpdated(Uri uri, JSONObject speaker, ContentResolver resolver)
        throws JSONException {
    final Cursor cursor = resolver.query(uri, SpeakersQuery.PROJECTION, null, null, null);
    try {//w  w  w.j a v  a2  s .  c  om
        if (!cursor.moveToFirst())
            return false;

        final String curFirstName = cursor.getString(SpeakersQuery.FIRST_NAME).toLowerCase().trim();
        final String curLastName = cursor.getString(SpeakersQuery.LAST_NAME).toLowerCase().trim();
        final String curBio = cursor.getString(SpeakersQuery.BIO).toLowerCase().trim();
        final String curCompany = cursor.getString(SpeakersQuery.COMPANY).toLowerCase().trim();
        final String newFirstName = speaker.has("firstName")
                ? speaker.getString("firstName").toLowerCase().trim()
                : curFirstName;
        final String newLastName = speaker.has("lastName") ? speaker.getString("lastName").toLowerCase().trim()
                : curLastName;
        final String newBio = speaker.has("bio") ? speaker.getString("bio").toLowerCase().trim() : curBio;
        final String newCompany = speaker.has("company") ? speaker.getString("company").toLowerCase().trim()
                : curCompany;

        return (!curFirstName.equals(newFirstName) || !curLastName.equals(newLastName) || !curBio.equals(newBio)
                || !curCompany.equals(newCompany));
    } finally {
        cursor.close();
    }
}

From source file:Main.java

public static LinkedList<Integer> retrieveIntegerFromCursor(Cursor cursor, String columnName) {
    if (null == cursor || 0 == cursor.getCount()) {
        return new LinkedList<Integer>();
    }/*  w  w w.j ava 2 s .c om*/

    LinkedList<Integer> ids = new LinkedList<Integer>();

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndexOrThrow(columnName));
            ids.add(new Integer(id));
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return ids;
}

From source file:com.frostwire.android.MediaScanner.java

private static long getSize(Context context, Uri uri) {
    Cursor c = null;
    try {//from w  w  w.  j a  v  a  2  s.  c o  m
        c = context.getContentResolver().query(uri, new String[] { "_size" }, null, null, null);

        if (c != null && c.moveToFirst()) {
            return c.getLong(0);
        }
    } catch (Throwable e) {
        LOG.error("Error getting file size for uri: " + uri, e);
    } finally {
        IOUtils.closeQuietly(c);
    }

    return 0;
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getRealPathFromUriApi19(Context context, Uri uri) {
    String filePath = "";
    String wholeId = DocumentsContract.getDocumentId(uri);

    // Split at colon, use second item in the array
    String id = wholeId.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String selection = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            selection, new String[] { id }, null);

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }/*from   ww  w  .  j  a va 2  s  .c  om*/
    cursor.close();
    return filePath;
}

From source file:com.goliathonline.android.kegbot.io.RemoteDrinksHandler.java

private static ContentValues queryDrinkDetails(Uri uri, ContentResolver resolver) {
    final ContentValues values = new ContentValues();
    final Cursor cursor = resolver.query(uri, SessionsQuery.PROJECTION, null, null, null);
    try {//from   w  ww  .ja v  a  2 s  .  co m
        if (cursor.moveToFirst()) {
            values.put(SyncColumns.UPDATED, cursor.getLong(SessionsQuery.UPDATED));
            values.put(Drinks.DRINK_STARRED, cursor.getInt(SessionsQuery.STARRED));
        } else {
            values.put(SyncColumns.UPDATED, KegbotContract.UPDATED_NEVER);
        }
    } finally {
        cursor.close();
    }
    return values;
}

From source file:Main.java

/**
 * Get a uri's file path//from   w w  w.  ja  v a 2s .c  om
 * 
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return the file path
 */
public static String getUriPath(Context context, Uri uri) {
    String filePath = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] projection = { MediaStore.Files.FileColumns.DATA };

        /* 
         * FIXME 2013-10-24 Tianzi Hou
         * 
         * we cannot get file path if it is from 
         *  content://com.google.android.gallery3d.provider
         * i.e. the Picasa service
         */
        filePath = null;
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(projection[0]);
            cursor.moveToFirst();
            filePath = cursor.getString(column_index);
            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        filePath = uri.getPath();
    }

    return filePath;
}

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

/**
 * Get all int values in the first column.
 *
 * @param close true to close the cursor or false to leave it open
 * @since 2.5.0//from   w  w w  .ja  v a 2s  .  c  o m
 */
public static int[] allInts(Cursor cursor, boolean close) {
    int[] i = EMPTY_INT_ARRAY;
    if (cursor.moveToFirst()) {
        i = new int[cursor.getCount()];
        do {
            i[cursor.getPosition()] = cursor.getInt(0);
        } while (cursor.moveToNext());
    }
    close(cursor, close);
    return i;
}