Example usage for android.database Cursor getCount

List of usage examples for android.database Cursor getCount

Introduction

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

Prototype

int getCount();

Source Link

Document

Returns the numbers of rows in the cursor.

Usage

From source file:Main.java

public static int getImageIdFromPath(Activity activity, String filePath) {
    String[] projection = { MediaStore.Images.Media._ID };
    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String where = String.format("_data like '%s' ", filePath);
    Cursor cursor = MediaStore.Images.Media.query(activity.getContentResolver(), uri, projection, where, null);
    int image_id = 0;
    if (cursor.getCount() != 0) {
        cursor.moveToFirst();//from w w  w .ja va  2s .c o m
        image_id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
        cursor.close();
    }
    return image_id;
}

From source file:Main.java

public static String CursorToJson(Cursor cursor) {
    do {/*from w w w.ja  v  a 2 s .co m*/
        if (!cursor.moveToNext()) {
            return "";
        }
        int i = 0;
        while (i < cursor.getCount()) {
            cursor.getString(0);
            i++;
        }
    } while (true);
}

From source file:Main.java

/**
 * method used to lookup the id of a contact photo id
 *
 * @param context/*  w  w  w.j a v  a  2  s  .co  m*/
 *            a context object used to get a content resolver
 * @param phoneNumber
 *            the phone number of the contact
 *
 * @return the id of the contact
 */
public static long lookupPhotoId(Context context, String phoneNumber) {

    long mPhotoId = -1;

    Uri mLookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

    String[] mProjection = new String[2];
    mProjection[0] = PhoneLookup._ID;
    mProjection[1] = PhoneLookup.PHOTO_ID;

    Cursor mCursor = context.getContentResolver().query(mLookupUri, mProjection, null, null, null);

    if (mCursor.getCount() > 0) {
        mCursor.moveToFirst();

        mPhotoId = mCursor.getLong(mCursor.getColumnIndex(PhoneLookup._ID));

        mCursor.close();
    }

    return mPhotoId;
}

From source file:Main.java

/**
 * Get specific row values from the database,
 * e.g. all years stored in the database or
 *      all months of a year stored in the database or
 *      all days in a month of a year stored in the database
 *
 * @param db// ww  w.  ja  v  a2  s. com
 *            pointer to database
 * @param requestField
 *            requested row from db
 *            "year" returns all year values found
 *            "month" returns all month values found in year <code>requestLimiterYear</code>
 *            "day" returns all day values found in month <code>requestLimiterMonth</code>
 *                                              and year <code>requestLimiterYear</code>
 * @param requestLimiterMonth
 *            limiter for request
 *            unused if requestField is "year"
 *            unused if requestField is "month"
 *            month if requestField is "day"
 * @param requestLimiterYear
 *            limiter for request
 *            unused if requestField is "year"
 *            year if requestField is "month"
 *            year if requestField is "day"
 *
 * @return <code>ArrayList<Integer></code>
 *            array list with all entries found
 */
public static ArrayList<Integer> getEntries(SQLiteDatabase db, String requestField, int requestLimiterMonth,
        int requestLimiterYear) {

    /** Array list holding the found values */
    ArrayList<Integer> returnArray = new ArrayList<>();

    /** Limiter for row search */
    String queryRequest = "select distinct " + requestField + " from " + TABLE_NAME;
    if (requestField.equalsIgnoreCase("day")) {
        queryRequest += " where month = " + String.valueOf(requestLimiterMonth) + " and year = "
                + String.valueOf(requestLimiterYear);
    } else if (requestField.equalsIgnoreCase("month")) {
        queryRequest += " where year = " + String.valueOf(requestLimiterYear);
    }

    /** Cursor holding the records of a day */
    Cursor allRows = db.rawQuery(queryRequest, null);

    allRows.moveToFirst();
    for (int i = 0; i < allRows.getCount(); i++) {
        returnArray.add(allRows.getInt(0));
        allRows.moveToNext();
    }
    allRows.close();
    return returnArray;
}

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();/*from   w ww . jav  a  2s .  c o  m*/
        int tinyImgId = tinyCursor.getInt(tinyCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
        tinyCursor.close();
        return Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                String.valueOf(tinyImgId));
    } else {
        tinyCursor.close();
        return null;
    }
}

From source file:Main.java

public static int getOrientation(Context context, Uri photoUri) {
    /* it's on the external media. */
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    if (cursor.getCount() != 1) {
        return -1;
    }/*w ww  .jav  a  2s .c o m*/

    cursor.moveToFirst();
    return cursor.getInt(0);
}

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.//from  w  ww.ja v a 2 s  .  c om
 */
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:Main.java

/** Returns true if there in the supplied database exists a row in the table 'tableName' where 'columnName'='columnValueWanted'.
 *
 * @param db A readable SQLiteDatabase object.
 * @param tableName The name of the table to scan. "FROM tableName"
 * @param columnName The name of the column
 * @param columnValueWanted The values you want to see if exists in a column.
 * @return Does the wanted value exist in the column in any row of the table in the database?
 *//*from w ww  .  j  a va2 s.  c om*/
public static boolean containsRowWithWhereStatement(SQLiteDatabase db, String tableName, String columnName,
        String columnValueWanted) {
    Cursor c = db.query(tableName, new String[] { columnName }, columnName + "=?",
            new String[] { columnValueWanted }, null, null, null, "1");
    boolean output = c.getCount() > 0;
    c.close();
    return output;
}

From source file:Main.java

public static boolean haveCalendars(Activity ctx) {

    boolean exists = false;

    String[] projection = new String[] { "_id", "name" };
    Uri calendars = Uri.parse("content://calendar/calendars");

    // Calendar uri changes for 2.2
    Uri calendars_2_2 = Uri.parse("content://com.android.calendar/calendars");
    Cursor managedCursor = ctx.managedQuery(calendars, projection, null, null, null);

    if (managedCursor == null || managedCursor.getCount() == 0)
        managedCursor = ctx.managedQuery(calendars_2_2, projection, null, null, null);

    if (managedCursor != null && managedCursor.moveToFirst()) {
        String calName;/*  w  w w .  j a  v  a2 s .com*/
        String calId;
        int nameColumn = managedCursor.getColumnIndex("name");
        int idColumn = managedCursor.getColumnIndex("_id");
        do {
            calName = managedCursor.getString(nameColumn);
            calId = managedCursor.getString(idColumn);
            exists = true;
        } while (managedCursor.moveToNext());

        managedCursor.close();
    }

    return exists;
}

From source file:Main.java

public static long[] getSongListForCursor(Cursor cursor) {
    if (cursor == null) {
        return sEmptyList;
    }//from w  ww . ja v a  2 s. co m
    int len = cursor.getCount();
    long[] list = new long[len];
    cursor.moveToFirst();
    int colidx = -1;
    try {
        colidx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID);
    } catch (IllegalArgumentException ex) {
        colidx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
    }
    for (int i = 0; i < len; i++) {
        list[i] = cursor.getLong(colidx);
        cursor.moveToNext();
    }
    return list;
}