Example usage for android.database Cursor getColumnIndex

List of usage examples for android.database Cursor getColumnIndex

Introduction

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

Prototype

int getColumnIndex(String columnName);

Source Link

Document

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

Usage

From source file:Main.java

/**
 * get image path by uri//from  www . ja v  a2s .c om
 *
 * @param context context
 * @param uri     uri
 * @return image path
 */
public static String query(Context context, Uri uri) {
    Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
            null);
    cursor.moveToNext();
    String path = cursor.getString(cursor.getColumnIndex(ImageColumns.DATA));
    cursor.close();
    return path;
}

From source file:disono.webmons.com.utilities.helpers.WBFile.java

/**
 * Get the real path from URI/*  w  ww  . j  a va  2s.  c o m*/
 *
 * @param activity
 * @param uri
 * @return
 */
public static String getRealPathFromURI(Activity activity, Uri uri) {
    Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
    assert cursor != null;
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getRealPathFromUriApi19(Context context, Uri uri) {
    String filePath = "";
    String wholeId = DocumentsContract.getDocumentId(uri);

    // Split at colon, use second item in the array
    String id = wholeId.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String selection = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            selection, new String[] { id }, null);

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }//from ww w  . j  a va 2  s. c o  m
    cursor.close();
    return filePath;
}

From source file:Main.java

/**
 * Looks up contact name in the address book by the phone number.
 * //from   ww  w. j av  a2 s.  c  o m
 * @param context the Android context.
 * @param phone the phone number to look up.
 * @return a contact name.
 */
public static String lookupNameByPhone(Context context, String phone) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
    String[] projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts._ID };
    Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

    try {
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if (name != null && name.length() > 0) {
                return name;
            }
        }
    } finally {
        cursor.close();
    }

    return phone;
}

From source file:Main.java

public static String getImagePath(Context context, String id) {
    String path = null;//from   w  w w. ja  v  a  2  s .co  m
    Cursor cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, Media._ID + "=?",
            new String[] { id }, null);
    while (cursor.moveToNext()) {
        path = cursor.getString(cursor.getColumnIndex(Media.DATA));
    }
    cursor.close();
    return path;
}

From source file:Main.java

private static Object getCursorValue(Cursor cursor, Field field, String columnName) {
    Class<?> fieldType = field.getType();

    if (fieldType.equals(int.class) || field.equals(Integer.class)) {
        return cursor.getInt(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(String.class)) {
        return cursor.getString(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(long.class) || field.equals(Long.class)) {
        return cursor.getLong(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(float.class) || field.equals(Float.class)) {
        return cursor.getFloat(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(double.class) || field.equals(Double.class)) {
        return cursor.getDouble(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(long.class) || field.equals(Long.class)) {
        return cursor.getLong(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(Date.class)) {
        long time = cursor.getLong(cursor.getColumnIndex(columnName));
        Date date = new Date(time);
        return date;
    }//from   w ww .ja  v a2 s .co m
    return cursor.getString(cursor.getColumnIndex(columnName));
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
    String filePath = "";
    if (DocumentsContract.isDocumentUri(context, uri)) {
        String wholeID = DocumentsContract.getDocumentId(uri);
        // Split at colon, use second item in the array
        String[] splits = wholeID.split(":");
        if (splits.length == 2) {
            String id = splits[1];

            String[] column = { MediaStore.Images.Media.DATA };
            // where id is equal to
            String sel = MediaStore.Images.Media._ID + "=?";
            Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    column, sel, new String[] { id }, null);
            int columnIndex = cursor.getColumnIndex(column[0]);
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(columnIndex);
            }//  ww w  . j  a  v  a 2  s  .  c  om
            cursor.close();
        }
    } else {
        filePath = uri.getPath();
    }
    return filePath;
}

From source file:Main.java

public static Uri getUriFromPath(Context context, String path) {

    String fileName = "file:///sdcard/DCIM/Camera/2013_07_07_12345.jpg";
    Uri fileUri = Uri.parse(fileName);//  ww  w.  jav a  2 s  .  co m
    String filePath = fileUri.getPath();
    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
            "_data = '" + filePath + "'", null, null);
    cursor.moveToNext();
    int id = cursor.getInt(cursor.getColumnIndex("_id"));
    Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);

    return uri;
}

From source file:Main.java

/**
 * Get the unread count for the given shortcut info
 * @param c cursor holding the unread counts
 * @param packageName the package we are looking for
 * @return int of the unread count/* w w w.  j a  va2 s  .  c o  m*/
 */
public static int getUnreadCount(Cursor c, String packageName) {
    int count = 0;

    try {
        if (c.moveToFirst()) {
            String currName = null;
            do {
                currName = c.getString(c.getColumnIndex("package_name"));
                if (currName.equals(packageName)) {
                    count = c.getInt(c.getColumnIndex("count"));
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {

    }

    return count;
}

From source file:Main.java

public static List<String> displayContacts(ContentResolver contentResolver) {

    List<String> contacts = new ArrayList<String>();
    ContentResolver cr = contentResolver;
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(
                    cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur
                            .getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                    contacts.add(name + " : " + phoneNo);
                }//from   w  w w  . j a  v  a2 s  . c o  m
                pCur.close();
            }
        }
    }
    return contacts;
}