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

/**
 * 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  www.  j a  va 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 = "_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

/**
 * 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.// ww 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.
 */
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 column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    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  w  w . j  a v a  2 s.c om
        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 (Exception e) {
        Log.d("android_utilities", "the error is " + e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:Main.java

public static LinkedList<String> retrieveStringFromCursor(Cursor cursor, String columnName) {
    LinkedList<String> result = new LinkedList<String>();

    if (null == cursor || 0 == cursor.getCount()) {
        return result;
    }// w  w w.  jav  a 2 s . c o  m

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            String str = cursor.getString(cursor.getColumnIndexOrThrow(columnName));
            result.add(str);
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return result;
}

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  . ja v  a2 s  .  com*/
        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 (Exception e) {
        //            FileLog.e("tmessages", e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:de.schildbach.wallet.data.ExchangeRatesProvider.java

public static ExchangeRate getExchangeRate(final Cursor cursor) {
    final String currencyCode = cursor
            .getString(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_CURRENCY_CODE));
    final Coin rateCoin = Coin
            .valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_RATE_COIN)));
    final Fiat rateFiat = Fiat.valueOf(currencyCode,
            cursor.getLong(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_RATE_FIAT)));
    final String source = cursor.getString(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_SOURCE));

    return new ExchangeRate(new org.bitcoinj.utils.ExchangeRate(rateCoin, rateFiat), source);
}

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 ww w . jav a  2 s. c  om
        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 (Exception e) {
        Log.e("tmessages", e.getMessage());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    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 {/*  ww w.  j  av 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);
            String value = cursor.getString(column_index);
            if (value.startsWith("content://") || !value.startsWith("/") && !value.startsWith("file://")) {
                return null;
            }
            return value;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:Main.java

public static LinkedList<Integer> retrieveIntegerFromCursor(Cursor cursor, String columnName) {
    if (null == cursor || 0 == cursor.getCount()) {
        return new LinkedList<Integer>();
    }//from  ww  w .  j  av a2 s  . com

    LinkedList<Integer> ids = new LinkedList<Integer>();

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndexOrThrow(columnName));
            ids.add(new Integer(id));
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return ids;
}

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  w ww .  j av  a2 s .  c  o m*/
    }

    return imagePath;
}