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:com.cyanogenmod.filemanager.util.MediaHelper.java

/**
 * Method that converts a content uri to a file system path
 *
 * @param cr The content resolver//from   ww  w .j a  va2s . 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.allen.mediautil.ImageTakerHelper.java

/**
 * ?//ww w  . j a  va2  s  . co m
 *
 * @param data 
 * @return ?
 */
public static String readBitmapFromAlbumResult(Context context, Intent data) {
    Uri imageUri = data.getData();
    if (imageUri == null) {
        return null;
    } else if (imageUri.toString().startsWith("file:///")) {
        try {
            // ??
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(imageUri.getPath(), options);
            return imageUri.getPath();
        } catch (Exception ex) {
            Toast.makeText(context.getApplicationContext(),
                    "?", Toast.LENGTH_SHORT).show();
            return null;
        }
    } else {
        String[] projection = { MediaStore.MediaColumns.DATA };
        CursorLoader cursorLoader = new CursorLoader(context, imageUri, projection, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        String selectedImagePath = cursor.getString(column_index);
        cursor.close();
        return selectedImagePath;
    }
}

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  w w  w .  j a v  a 2s  . 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 = MediaColumns.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./* w w w  . jav 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 = "_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.fede.Utilities.GeneralUtils.java

public static String getNameFromNumber(String number, Context c) throws NameNotFoundException {
    String name;/*w  w w .  jav a2s .  c  o m*/
    String[] columns = { ContactsContract.PhoneLookup.DISPLAY_NAME };

    Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number);
    Cursor idCursor = c.getContentResolver().query(lookupUri, columns, null, null, null);
    if (idCursor.moveToFirst()) {
        int nameIdx = idCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME);
        name = idCursor.getString(nameIdx);
    } else {
        throw new NameNotFoundException(number);
    }
    idCursor.close();
    return name;
}

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   ww w .  j  a va 2s .co  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: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  .ja v a2 s.co  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

/**
 * 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 va 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:com.example.app_2.utils.Utils.java

public static String getPath(Context context, Uri uri) {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {/*w w w . ja v a2  s.c o m*/
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    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.//from   w  ww  .  j av a  2 s.  com
 * @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.
 * @author paulburke
 */
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()) {
            if (DEBUG)
                DatabaseUtils.dumpCursor(cursor);

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