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:org.thoughtcrime.securesms.mms.MmsCommunication.java

protected static MmsConnectionParameters getMmsConnectionParameters(Context context) throws MmsException {
    Cursor cursor = DatabaseFactory.getMmsDatabase(context).getCarrierMmsInformation();

    try {//from   www  .j ava2  s.  c o  m
        if (cursor == null || !cursor.moveToFirst())
            throw new MmsException("No carrier MMS information available.");

        do {
            String mmsc = cursor.getString(cursor.getColumnIndexOrThrow("mmsc"));
            String proxy = cursor.getString(cursor.getColumnIndexOrThrow("mmsproxy"));
            String port = cursor.getString(cursor.getColumnIndexOrThrow("mmsport"));

            if (mmsc != null && !mmsc.equals(""))
                return new MmsConnectionParameters(mmsc, proxy, port);

        } while (cursor.moveToNext());

        throw new MmsException("No carrier MMS information available.");
    } finally {
        if (cursor != null)
            cursor.close();
    }
}

From source file:free.yhc.netmbuddy.share.Json.java

static JSONObject playlistToJson(long plid) {
    final int COLI_ID = 0;
    Cursor c = DB.get().queryVideos(plid, new ColVideo[] { ColVideo.ID }, null, false);
    if (!c.moveToFirst()) {
        c.close();/*from   w  ww . java  2 s .  com*/
        return null;
    }

    JSONObject jo = new JSONObject();
    try {
        jo.put(FTITLE, DB.get().getPlaylistInfo(plid, ColPlaylist.TITLE));
        String thumbnailYtvid = (String) DB.get().getPlaylistInfo(plid, ColPlaylist.THUMBNAIL_YTVID);
        if (Utils.isValidValue(thumbnailYtvid))
            jo.put(FTHUMBNAIL_YTVID, thumbnailYtvid);

        JSONArray jarr = new JSONArray();
        do {
            JSONObject jov = videoToJson(c.getLong(COLI_ID));
            eAssert(null != jov);
            jarr.put(jov);
        } while (c.moveToNext());
        jo.put(FVIDEOS, jarr);
    } catch (JSONException e) {
        jo = null;
    }
    c.close();

    return jo;
}

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./* w w w  .  j a  v  a2s.  c  om*/
 * @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.
 */
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()) {
            final int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

public static int getOrientation(Context context, Uri photoUri) {
    int orientation = 0;
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
    if (cursor != null) {
        if (cursor.getCount() != 1) {
            return -1;
        }/*  w  w w . j ava  2 s.co  m*/
        cursor.moveToFirst();
        orientation = cursor.getInt(0);
        cursor.close();
    }
    return orientation;
}

From source file:Main.java

public static String getRealPathFromURI(Uri contentUri, ContentResolver content_resolver) {
    Cursor cursor = null;
    try {//w w  w.ja v a 2  s .c  om
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = content_resolver.query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

public static boolean isMediaScannerScanning(Context context) {
    boolean result = false;

    Cursor cursor = query(context, MediaStore.getMediaScannerUri(),
            new String[] { MediaStore.MEDIA_SCANNER_VOLUME }, null, null, null);
    if (cursor != null) {
        if (cursor.getCount() == 1) {
            cursor.moveToFirst();
            result = "external".equals(cursor.getString(0));
        }/*  w  w  w  .  j av a 2  s . c  o  m*/
        cursor.close();
    }

    return result;
}

From source file:ca.mudar.parkcatcher.utils.ParserUtils.java

/**
 * Query and return the newest {@link SyncColumns#UPDATED} time for all
 * entries under the requested {@link Uri}. Expects the {@link Uri} to
 * reference a directory of several items.
 *//*from w ww . jav a 2s.c o m*/
public static long queryDirUpdated(Uri uri, ContentResolver resolver) {
    final String[] projection = { "MAX(" + SyncColumns.UPDATED + ")" };
    final Cursor cursor = resolver.query(uri, projection, null, null, null);
    try {
        cursor.moveToFirst();
        return cursor.getLong(0);
    } finally {
        cursor.close();
    }
}

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 .  jav  a2 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. 
 */
private 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()) {
            final int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

/**
 * Handles V19 and up uri's//ww  w. ja v  a  2  s  . c o  m
 * @param context
 * @param contentUri
 * @return path
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getPathForV19AndUp(Context context, Uri contentUri) {
    String wholeID = DocumentsContract.getDocumentId(contentUri);

    // 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);

    String filePath = "";
    int columnIndex = cursor.getColumnIndex(column[0]);
    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }

    cursor.close();
    return filePath;
}

From source file:Main.java

/**
 * Get the file path from a Media storage URI.
 */// www .  ja v  a 2s  .  co m
public static String getPathFromURI(ContentResolver contentResolver, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = contentResolver.query(contentUri, proj, null, null, null);
    if (cursor == null) {
        return null;
    }
    try {
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (!cursor.moveToFirst()) {
            return null;
        } else {
            return cursor.getString(columnIndex);
        }
    } finally {
        cursor.close();
    }
}