Example usage for android.database Cursor getColumnIndexOrThrow

List of usage examples for android.database Cursor getColumnIndexOrThrow

Introduction

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

Prototype

int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;

Source Link

Document

Returns the zero-based index for the given column name, or throws IllegalArgumentException if the column doesn't exist.

Usage

From source file:Main.java

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };
    try {/*from w w w. j  a v  a2  s  .com*/
        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 String getFirstCNLetterByContactId(Context context, long contactId) {
    String result = "";
    String[] projection = {//from  w w  w  .ja va  2  s.  c  o m
            // Data._ID,
            // Data.DISPLAY_NAME,
            // Data.CONTACT_ID,
            Data.DATA12, };
    String where = Data.CONTACT_ID + "=?";
    final String sortOrder = null;
    Cursor cursor = context.getContentResolver().query(PHONE_CONTACTS_URI, projection, where,
            new String[] { String.valueOf(contactId) }, sortOrder);

    if (cursor != null) {
        try {
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String nameLetters = cursor.getString(cursor.getColumnIndexOrThrow("data12"));
                    if (null != nameLetters && !"".equals(nameLetters)) {
                        result = nameLetters.substring(0, 1).toUpperCase();
                        break;
                    }
                }
            }

        } finally {
            cursor.close();
        }
    }

    return result;
}

From source file:com.coinomi.wallet.ExchangeRatesProvider.java

public static String getCurrencyCodeId(@Nonnull final Cursor cursor) {
    return cursor.getString(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_CURRENCY_ID));
}

From source file:com.bushstar.htmlcoin_android_wallet.ExchangeRatesProvider.java

public static ExchangeRate getExchangeRate(@Nonnull final Cursor cursor) {
    final String currencyCode = cursor
            .getString(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_CURRENCY_CODE));
    final BigInteger rate = BigInteger
            .valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_RATE)));
    final String source = cursor.getString(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_SOURCE));

    return new ExchangeRate(currencyCode, rate, source);
}

From source file:Main.java

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 {/*from w  w  w .ja v  a2  s .  co m*/
        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;
}

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

/**
 * Method that converts a file reference to a content uri reference
 *
 * @param cr A content resolver/*from  w w w.  j  a  va 2  s  . c o m*/
 * @param path The path to search
 * @param volume The volume
 * @return Uri The content uri or null if file not exists in the media database
 */
private static Uri fileToContentUri(ContentResolver cr, String path, String volume) {
    final String[] projection = { BaseColumns._ID, MediaStore.Files.FileColumns.MEDIA_TYPE };
    final String where = MediaColumns.DATA + " = ?";
    Uri baseUri = MediaStore.Files.getContentUri(volume);
    Cursor c = cr.query(baseUri, projection, where, new String[] { path }, null);
    try {
        if (c != null && c.moveToNext()) {
            int type = c.getInt(c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MEDIA_TYPE));
            if (type != 0) {
                // Do not force to use content uri for no media files
                long id = c.getLong(c.getColumnIndexOrThrow(BaseColumns._ID));
                return Uri.withAppendedPath(baseUri, String.valueOf(id));
            }
        }
    } finally {
        IOUtils.closeQuietly(c);
    }
    return null;
}

From source file:Main.java

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

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

    try {/*  w ww .  j a v  a 2 s  .c  o m*/
        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;
}

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 www . j av a  2  s .co  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:com.maskyn.fileeditorpro.util.AccessStorageApi.java

public static String getName(Context context, Uri uri) {

    if (uri == null || uri.equals(Uri.EMPTY))
        return "";

    String fileName = "";
    try {//from w w w.  j  a v a  2 s  .co  m
        String scheme = uri.getScheme();
        if (scheme.equals("file")) {
            fileName = uri.getLastPathSegment();
        } else if (scheme.equals("content")) {
            String[] proj = { MediaStore.Images.Media.DISPLAY_NAME };
            Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
            if (cursor != null && cursor.getCount() != 0) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
                cursor.moveToFirst();
                fileName = cursor.getString(columnIndex);
            }
            if (cursor != null) {
                cursor.close();
            }
        }
    } catch (Exception ex) {
        return "";
    }
    return fileName;
}

From source file:Main.java

public static int getMessageTypeByThreadAndBody(Context context, long threadId, String body) {
    int type = 1;
    final String[] projection = new String[] { " sms.type " + " from sms " + " where sms.thread_id = "
            + threadId + " and sms.body = '" + body + "' --" };
    // Create cursor
    /*/*w w w  . j ava  2 s . c om*/
     * Cursor cursor = context.getContentResolver().query( SMS_CONTENT_URI,
     * projection, null, null, null);
     */
    Cursor cursor = context.getContentResolver().query(SMS_CONTENT_URI, new String[] { "type" },
            "sms.thread_id = ? and sms.body = ?", new String[] { String.valueOf(threadId), body }, null);

    if (cursor != null) {
        try {
            if (cursor.getCount() > 0) {

                while (cursor.moveToNext()) {
                    type = cursor.getInt(cursor.getColumnIndexOrThrow("type"));
                    return type;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cursor.close();
        }
    }

    return type;
}