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

@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 ww.  j ava2s. co  m*/
    }

    return imagePath;
}

From source file:Main.java

/**
 * Consolidates the file path determination functionality of the various
 * media prompts. Beginning with KitKat, the responses use a different
 * mechanism and needs a lot of special handling.
 *
 * @param ctxt// ww  w  . j  a va 2 s . co  m
 * @param uri
 * @param pathKey
 * @return
 */
@SuppressLint("NewApi")
public static String getPathFromUri(Context ctxt, Uri uri, String pathKey) {

    if (Build.VERSION.SDK_INT >= 19) {
        return getPath(ctxt, uri);
    } else {
        if (uri.toString().startsWith("file")) {
            return uri.toString().substring(7);
        } else {
            String[] projection = { pathKey };
            Cursor c = null;
            try {
                c = ctxt.getContentResolver().query(uri, projection, null, null, null);
                int column_index = c.getColumnIndexOrThrow(pathKey);
                String path = null;
                if (c.getCount() > 0) {
                    c.moveToFirst();
                    path = c.getString(column_index);
                }
                return path;
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

From source file:Main.java

public static String getRealPathFromURI(Context context, long album_id) {
    Cursor cursor = null;
    try {/*from w  w  w  .jav  a2  s  .c  o m*/
        cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Audio.Albums.ALBUM_ART }, MediaStore.Audio.Albums._ID + "=?",
                new String[] { String.valueOf(album_id) }, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART);
        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.//from   ww w  .j  a  v  a  2s  .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 = 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.
 * //  w  w w  . j av  a2s.  c  om
 * @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;
}

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

public static ExchangeRate getExchangeRate(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

public static String getFilePathByContentResolver(Context context, Uri uri) {
    if (null == uri) {
        return null;
    }//www. ja v  a  2s  . c om
    Cursor c = context.getContentResolver().query(uri, null, null, null, null);
    String filePath = null;
    if (null == c) {
        throw new IllegalArgumentException("Query on " + uri + " returns null result.");
    }
    try {
        if ((c.getCount() != 1) || !c.moveToFirst()) {
        } else {
            filePath = c.getString(c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
        }
    } finally {
        c.close();
    }
    return filePath;
}

From source file:Main.java

private static String fileUriTitle(Context context, String contentUri) {
    String result = null;//w  w  w .  j a  v  a  2 s .  c o m
    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

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);//from w ww.j a  v a2 s. c o  m
    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

public static String getFirstCNLetterByContactId8(Context context, long contactId) {
    String result = "";
    String[] projection = { "_id", "display_name", "data1", "sort_key" };
    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 {//from  w  ww . j  a  v a 2s  .c om
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String nameLetters = cursor.getString(cursor.getColumnIndexOrThrow("sort_key"));
                    if (null != nameLetters && !"".equals(nameLetters)) {
                        result = nameLetters.substring(0, 1).toUpperCase();
                        break;
                    }
                }
            }

        } finally {
            cursor.close();
        }
    }

    return result;
}