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 a uri's user-friendly display name
 * /*from w  w  w .ja  v  a 2 s .  c  om*/
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return a user-friendly display name
 */
public static String getUriDisplayName(Context context, Uri uri) {
    String displayName = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] proj = { OpenableColumns.DISPLAY_NAME };
        Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);

        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
            cursor.moveToFirst();
            displayName = cursor.getString(columnIndex);

            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        displayName = uri.getLastPathSegment();
    }

    return displayName;
}

From source file:Main.java

public static String getPathFromUri(Context context, Uri u) {
    String[] pojo = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(u, pojo, null, null, null);
    String picPath = null;//  w  ww. j av a2  s . com
    if (cursor != null) {
        int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
        cursor.moveToFirst();
        picPath = cursor.getString(columnIndex);
        return picPath;
    }
    return null;
}

From source file:Main.java

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

        try {/*ww  w.j  a  va  2 s  .  co  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) {
            // Eat it
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

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

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {//from w  w  w. ja va 2 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) {

        }
    }

    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

public static File uri2File(Activity context, Uri uri) {
    File file;//from  w  w w  .  ja  v a 2  s.c o m
    String[] project = { MediaStore.Images.Media.DATA };
    Cursor actualImageCursor = context.getContentResolver().query(uri, project, null, null, null);
    if (actualImageCursor != null) {
        int actual_image_column_index = actualImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualImageCursor.moveToFirst();
        String img_path = actualImageCursor.getString(actual_image_column_index);
        file = new File(img_path);
    } else {
        file = new File(uri.getPath());
    }
    if (actualImageCursor != null)
        actualImageCursor.close();
    return file;
}

From source file:edu.mit.mobile.android.locast.data.tags.TaggableUtils.java

public static Set<String> getTags(ContentProviderClient cpc, Uri tagDir) throws RemoteException {
    final Set<String> tags = new HashSet<String>();

    final Cursor c = cpc.query(tagDir, TAG_PROJECTION, null, null, null);

    try {//w ww .  j a  v  a  2 s.  co  m
        final int tagNameCol = c.getColumnIndexOrThrow(Tag.COL_NAME);

        while (c.moveToNext()) {
            tags.add(c.getString(tagNameCol));
        }
    } finally {
        c.close();
    }

    return tags;
}

From source file:Main.java

public static String getRealPathByUri(Context context, Uri uri) {
    if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        return uri.getPath();
    }/*  w  w  w .j a v  a  2 s  .co m*/

    try {
        ContentResolver resolver = context.getContentResolver();
        String[] proj = new String[] { MediaStore.Images.Media.DATA };
        Cursor cursor = MediaStore.Images.Media.query(resolver, uri, proj);
        String realPath = null;
        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.getCount() > 0 && cursor.moveToFirst()) {
                realPath = cursor.getString(columnIndex);
            }
            cursor.close();
        }
        return realPath;
    } catch (Exception e) {
        return uri.getPath();
    }
}

From source file:Main.java

/**
 * Get a uri's file path/*from   www  .j  a  v  a 2s  . co  m*/
 * 
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return the file path
 */
public static String getUriPath(Context context, Uri uri) {
    String filePath = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] projection = { MediaStore.Files.FileColumns.DATA };

        /* 
         * FIXME 2013-10-24 Tianzi Hou
         * 
         * we cannot get file path if it is from 
         *  content://com.google.android.gallery3d.provider
         * i.e. the Picasa service
         */
        filePath = null;
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(projection[0]);
            cursor.moveToFirst();
            filePath = cursor.getString(column_index);
            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        filePath = uri.getPath();
    }

    return filePath;
}

From source file:Main.java

/**
 * Handles pre V19 uri's//from   w  w  w .java 2s.c o m
 * @param context
 * @param contentUri
 * @return
 */
private static String getPathForPreV19(Context context, Uri contentUri) {
    String res = null;

    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            res = cursor.getString(column_index);
        }
        cursor.close();
    }

    return res;
}

From source file:Main.java

public static String getNewThreadID(Context context) {
    int new_id = 0;
    final String[] projection = new String[] { "_id" };
    Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
    Cursor query = context.getContentResolver().query(uri, projection, null, null, "date DESC");
    if (query != null) {
        while (query.moveToNext()) {
            int tmp = query.getInt(query.getColumnIndexOrThrow("_id")) + 1;
            if (tmp > new_id) {
                new_id = tmp;
            }// w  w  w.  j  av a2 s  . c  o m
        }
        query.close();
    }
    return "" + new_id;
}