Example usage for android.database Cursor getString

List of usage examples for android.database Cursor getString

Introduction

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

Prototype

String getString(int columnIndex);

Source Link

Document

Returns the value of the requested column as a String.

Usage

From source file:cn.edu.wyu.documentviewer.model.DocumentInfo.java

/**
 * Missing or null values are returned as -1.
 *///from   w  ww  .  jav a2  s. c  o  m
public static long getCursorLong(Cursor cursor, String columnName) {
    final int index = cursor.getColumnIndex(columnName);
    if (index == -1)
        return -1;
    final String value = cursor.getString(index);
    if (value == null)
        return -1;
    try {
        return Long.parseLong(value);
    } catch (NumberFormatException e) {
        return -1;
    }
}

From source file:com.cyanogenmod.filemanager.util.MediaHelper.java

/**
 * Method that converts a content uri to a file system path
 *
 * @param cr The content resolver//  w  w  w.  j a va  2s .  c  o m
 * @param id The media database id
 * @param volume The volume
 * @return File The file reference
 */
private static File mediaIdToFile(ContentResolver cr, long id, String volume) {
    final String[] projection = { MediaColumns.DATA };
    final String where = MediaColumns._ID + " = ?";
    Uri baseUri = MediaStore.Files.getContentUri(volume);
    Cursor c = cr.query(baseUri, projection, where, new String[] { String.valueOf(id) }, null);
    try {
        if (c != null && c.moveToNext()) {
            return new File(c.getString(c.getColumnIndexOrThrow(MediaColumns.DATA)));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return null;
}

From source file:com.markupartist.sthlmtraveling.FavoritesFragment.java

private static JourneyQuery getJourneyQuery(Cursor cursor) {
    // TODO: Investigate if we can add some kind of caching here.
    String jsonJourneyQuery = cursor.getString(COLUMN_INDEX_JOURNEY_DATA);
    JourneyQuery journeyQuery = null;/*from   w  w  w  .  ja  v a2  s .c  o  m*/

    try {
        journeyQuery = JourneyQuery.fromJson(new JSONObject(jsonJourneyQuery));
    } catch (JSONException e) {
        Log.e(TAG, "Failed to covert to journey from json.");
    }
    return journeyQuery;
}

From source file:Main.java

public static ArrayList<String> findSmsByAddress(Context context, String address) {
    ArrayList<String> list = new ArrayList<String>();
    try {//from w  w  w . j  a va2 s.  c  om
        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

/**
 * Try to return the absolute file path from the given Uri
 *
 * @param context/*from  w  w  w .j a  va  2s  .  c  o m*/
 * @param uri
 * @return the file path or null
 */
public static String uri2FilePath(final Context context, final Uri uri) {
    if (null == uri)
        return null;
    final String scheme = uri.getScheme();
    String data = null;
    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri,
                new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:Main.java

public static String logCursor(String prefix, Cursor cr) {
    StringBuilder sb = new StringBuilder().append(prefix + ": ");
    for (int i = 0; i < cr.getColumnCount(); i++) {
        sb.append(cr.getColumnName(i)).append("=");
        switch (cr.getType(i)) {
        case Cursor.FIELD_TYPE_NULL:
            sb.append("NULL");
            break;
        case Cursor.FIELD_TYPE_STRING:
            sb.append("\"").append(cr.getString(i)).append("\"");
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            sb.append(cr.getInt(i));//from   w w  w  .  ja v  a 2s .  com
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            sb.append(cr.getFloat(i));
            break;
        case Cursor.FIELD_TYPE_BLOB:
            sb.append("BIN(").append(cr.getBlob(i).length).append("b)");
            break;
        }
        sb.append(" ");
    }
    return sb.toString();
}

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  w w w.j av a 2s. c  om
        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:Main.java

public static String getPathFromUri(Context context, Uri uri) {
    if (uri == null) {
        return null;
    }/*  w ww. j a v a2 s. co m*/
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor == null) {
            return 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

/**
 * 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 ww .  j a  v  a  2  s  .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.
 */
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    final String column = MediaStore.Images.Media.DATA;
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

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.
 * /*from ww w .  ja  v a 2  s  .  c  o m*/
 * @param context
 *            The context.
 * @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 = MediaStore.MediaColumns.DATA;
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);

            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}