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:com.csipsimple.backup.SipProfileJson.java

public static JSONObject serializeSipProfile(Context context, SipProfile profile) {
    JSONObject jsonProfile = serializeBaseSipProfile(profile);
    JSONArray jsonFilters = new JSONArray();

    Cursor c = Filter.getFiltersCursorForAccount(context, profile.id);
    int numRows = c.getCount();
    c.moveToFirst();
    for (int i = 0; i < numRows; ++i) {
        Filter f = new Filter(c);
        try {// w  w  w  . j a  v a2  s.  c  o  m
            jsonFilters.put(i, serializeBaseFilter(f));
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Impossible to add fitler", e);
        }
        c.moveToNext();
    }
    c.close();

    try {
        jsonProfile.put(FILTER_KEY, jsonFilters);
    } catch (JSONException e) {
        Log.e(THIS_FILE, "Impossible to add fitlers", e);
    }

    return jsonProfile;
}

From source file:Main.java

public static String getAbsoluteImagePath(Activity context, Uri uri) {
    String imagePath = "";
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.managedQuery(uri, proj, // Which columns to
            // return
            null, // WHERE clause; which rows to return (all rows)
            null, // WHERE clause selection arguments (none)
            null); // Order-by clause (ascending by name)

    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (cursor.getCount() > 0 && cursor.moveToFirst()) {
            imagePath = cursor.getString(column_index);
        }//from   www .ja  v  a  2s  .c o  m
    }

    return imagePath;
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static String getAbsoluteImagePath(Activity context, Uri uri) {
    String imagePath = "";
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.managedQuery(uri, proj, // Which columns to
            // return
            null, // WHERE clause; which rows to return (all rows)
            null, // WHERE clause selection arguments (none)
            null); // Order-by clause (ascending by name)

    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (cursor.getCount() > 0 && cursor.moveToFirst()) {
            imagePath = cursor.getString(column_index);
        }/*from   w  w w . ja  v a  2s  . c o  m*/
    }

    return imagePath;
}

From source file:Main.java

private static long fileUriFileSize(Context context, String contentUri) {
    long result = 0;
    String[] p = { MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.SIZE };
    Uri uri = Uri.parse(contentUri);// ww w.j a  v a2s  .  c om
    String path = uri.getPath();
    String last = Uri.parse(path).getLastPathSegment();
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, p, // which columns
            MediaStore.MediaColumns.DISPLAY_NAME + "='" + last + "'", // which rows
            null, // selection args (none)
            null); // order-by clause (ascending by name)
    if (cursor != null) {
        int scol = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
        if (cursor.moveToFirst()) {
            result = cursor.getLong(scol);
        }
    }
    return (result);
}

From source file:Main.java

private static String fileUriTitle(Context context, String contentUri) {
    String result = null;/* w  ww .j a v  a 2s  . c om*/
    String[] p = { MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE };
    Uri uri = Uri.parse(contentUri);
    String path = uri.getPath();
    String last = Uri.parse(path).getLastPathSegment();
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, p, // which columns
            MediaStore.MediaColumns.DISPLAY_NAME + "='" + last + "'", // which rows
            null, // selection args (none)
            null); // order-by clause (ascending by name)
    if (cursor != null) {
        int tcol = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
        if (cursor.moveToFirst()) {
            result = cursor.getString(tcol);
        }
    }
    return (result);
}

From source file:Main.java

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context       The context./*from  w w  w.j  a  v  a 2 s. c  o m*/
 * @param uri           The Uri to query.
 * @param selection     (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 * @author paulburke
 */
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            if (DEBUG)
                DatabaseUtils.dumpCursor(cursor);

            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } catch (IllegalArgumentException ex) {
        Log.i(TAG, "getDataColumn: _data", ex);
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:com.trk.aboutme.facebook.Settings.java

/**
 * Acquire the current attribution id from the facebook app.
 * @return returns null if the facebook app is not present on the phone.
 *///from  www.  java2 s .c  o m
public static String getAttributionId(ContentResolver contentResolver) {
    String[] projection = { ATTRIBUTION_ID_COLUMN_NAME };
    Cursor c = contentResolver.query(ATTRIBUTION_ID_CONTENT_URI, projection, null, null, null);
    if (c == null || !c.moveToFirst()) {
        return null;
    }
    String attributionId = c.getString(c.getColumnIndex(ATTRIBUTION_ID_COLUMN_NAME));
    c.close();
    return attributionId;
}

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

private static boolean isSessionUpdated(Uri uri, JSONObject session, ContentResolver resolver)
        throws JSONException {
    final Cursor cursor = resolver.query(uri, SessionsQuery.PROJECTION, null, null, null);
    try {//from w  w w  .j  a  v a 2 s.  c  o  m
        if (!cursor.moveToFirst())
            return false;

        final String curTitle = cursor.getString(SessionsQuery.TITLE).toLowerCase().trim();
        final String curSummary = cursor.getString(SessionsQuery.SUMMARY).toLowerCase().trim();
        final String curExperience = cursor.getString(SessionsQuery.EXPERIENCE).toLowerCase().trim();
        final String curType = cursor.getString(SessionsQuery.TYPE).toLowerCase().trim();
        final String newTitle = session.getString("title").toLowerCase().trim();
        final String newSummary = session.getString("summary").toLowerCase().trim();
        final String newExperience = session.getString("experience").toLowerCase().trim();
        final String newType = session.getString("type").toLowerCase().trim();

        return (!curTitle.equals(newTitle) || !curSummary.equals(newSummary)
                || !curExperience.equals(newExperience) || !curType.equals(newType));
    } finally {
        cursor.close();
    }
}

From source file:jog.my.memory.gcm.ServerUtilities.java

/**
 * Gets the real path of the URI returned from the camera
 * @param contentUri - apparent URI of resource
 * @return - actual URI of resource//from  w  w w . j  a v  a 2  s . c  o  m
 */
public static String getRealPathFromURI(Uri contentUri, Context context) {
    String[] proj = new String[] { MediaStore.Images.ImageColumns.DATA };

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

    String filename = cursor.getString(column_index);
    cursor.close();

    return filename;
}

From source file:za.co.neilson.alarm.database.Database.java

public static Alarm getAlarm(int id) {
    // TODO Auto-generated method stub
    String[] columns = new String[] { COLUMN_ALARM_ID, COLUMN_ALARM_ACTIVE, COLUMN_ALARM_TIME,
            COLUMN_ALARM_DAYS, COLUMN_ALARM_DIFFICULTY, COLUMN_ALARM_TONE, COLUMN_ALARM_VIBRATE,
            COLUMN_ALARM_NAME };// w  w w . j  av  a2 s. c  o m
    Cursor c = getDatabase().query(ALARM_TABLE, columns, COLUMN_ALARM_ID + "=" + id, null, null, null, null);
    Alarm alarm = null;

    if (c.moveToFirst()) {

        alarm = new Alarm();
        alarm.setId(c.getInt(1));
        alarm.setAlarmActive(c.getInt(2) == 1);
        alarm.setAlarmTime(c.getString(3));
        byte[] repeatDaysBytes = c.getBlob(4);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(repeatDaysBytes);
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Alarm.Day[] repeatDays;
            Object object = objectInputStream.readObject();
            if (object instanceof Alarm.Day[]) {
                repeatDays = (Alarm.Day[]) object;
                alarm.setDays(repeatDays);
            }
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        alarm.setDifficulty(Difficulty.values()[c.getInt(5)]);
        alarm.setAlarmTonePath(c.getString(6));
        alarm.setVibrate(c.getInt(7) == 1);
        alarm.setAlarmName(c.getString(8));
    }
    c.close();
    return alarm;
}