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:com.github.chenxiaolong.dualbootpatcher.FileUtils.java

public static UriMetadata[] queryUriMetadata(ContentResolver cr, Uri... uris) {
    ThreadUtils.enforceExecutionOnNonMainThread();

    UriMetadata[] metadatas = new UriMetadata[uris.length];
    for (int i = 0; i < metadatas.length; i++) {
        UriMetadata metadata = new UriMetadata();
        metadatas[i] = metadata;/*  ww  w .j  av  a 2 s  .c om*/
        metadata.uri = uris[i];
        metadata.mimeType = cr.getType(metadata.uri);

        if (SAF_SCHEME.equals(metadata.uri.getScheme())) {
            Cursor cursor = cr.query(metadata.uri, null, null, null, null, null);
            try {
                if (cursor != null && cursor.moveToFirst()) {
                    int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                    int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);

                    metadata.displayName = cursor.getString(nameIndex);
                    if (cursor.isNull(sizeIndex)) {
                        metadata.size = -1;
                    } else {
                        metadata.size = cursor.getLong(sizeIndex);
                    }
                }
            } finally {
                IOUtils.closeQuietly(cursor);
            }
        } else if (FILE_SCHEME.equals(metadata.uri.getScheme())) {
            metadata.displayName = metadata.uri.getLastPathSegment();
            metadata.size = new File(metadata.uri.getPath()).length();
        } else {
            throw new IllegalArgumentException("Cannot handle URI: " + metadata.uri);
        }
    }

    return metadatas;
}

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

/**
 * @param cr//from w w w.  j a v  a 2s.c  om
 * @param item
 * @param prefix
 * @return a list of all the tags attached to a given item
 */
public static Set<String> getTags(ContentResolver cr, Uri item, String prefix) {
    final Cursor tags = cr.query(Uri.withAppendedPath(item, Tag.PATH), Tag.DEFAULT_PROJECTION, null, null,
            null);
    final Set<String> tagSet = new HashSet<String>(tags.getCount());
    final int tagColumn = tags.getColumnIndex(Tag._NAME);
    final Predicate<String> predicate = getPrefixPredicate(prefix);
    for (tags.moveToFirst(); !tags.isAfterLast(); tags.moveToNext()) {
        final String tag = tags.getString(tagColumn);
        if (predicate.apply(tag)) {
            final int separatorIndex = tag.indexOf(PREFIX_SEPARATOR);
            if (separatorIndex == -1) {
                tagSet.add(tag);
            } else {
                tagSet.add(tag.substring(separatorIndex + 1));
            }
        }
    }
    tags.close();
    return tagSet;
}

From source file:Main.java

/**
 * Try to get the exif orientation of the passed image uri
 *
 * @param context/*from ww w.j  a v  a2 s.co  m*/
 * @param uri
 * @return
 */
public static int getExifOrientation(Context context, Uri uri) {

    final String scheme = uri.getScheme();

    ContentProviderClient provider = null;
    if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) {
        return getExifOrientation(uri.getPath());
    } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
        try {
            provider = context.getContentResolver().acquireContentProviderClient(uri);
        } catch (SecurityException e) {
            return 0;
        }

        if (provider != null) {
            Cursor result;
            try {
                result = provider.query(uri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION,
                        MediaStore.Images.ImageColumns.DATA }, null, null, null);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }

            if (result == null) {
                return 0;
            }

            int orientationColumnIndex = result.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION);
            int dataColumnIndex = result.getColumnIndex(MediaStore.Images.ImageColumns.DATA);

            try {
                if (result.getCount() > 0) {
                    result.moveToFirst();

                    int rotation = 0;

                    if (orientationColumnIndex > -1) {
                        rotation = result.getInt(orientationColumnIndex);
                    }

                    if (dataColumnIndex > -1) {
                        String path = result.getString(dataColumnIndex);
                        rotation |= getExifOrientation(path);
                    }
                    return rotation;
                }
            } finally {
                result.close();
            }
        }
    }
    return 0;
}

From source file:Main.java

/**
 * Try to get the exif orientation of the passed image uri
 * /*from ww w.ja  v  a  2 s  . c o m*/
 * @param context
 * @param uri
 * @return
 */
public static int getExifOrientation(Context context, Uri uri) {

    final String scheme = uri.getScheme();

    ContentProviderClient provider = null;
    if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) {
        return getExifOrientation(uri.getPath());
    } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
        try {
            provider = context.getContentResolver().acquireContentProviderClient(uri);
        } catch (SecurityException e) {
            return 0;
        }

        if (provider != null) {
            Cursor result;
            try {
                result = provider.query(uri,
                        new String[] { Images.ImageColumns.ORIENTATION, Images.ImageColumns.DATA }, null, null,
                        null);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }

            if (result == null) {
                return 0;
            }

            int orientationColumnIndex = result.getColumnIndex(Images.ImageColumns.ORIENTATION);
            int dataColumnIndex = result.getColumnIndex(Images.ImageColumns.DATA);

            try {
                if (result.getCount() > 0) {
                    result.moveToFirst();

                    int rotation = 0;

                    if (orientationColumnIndex > -1) {
                        rotation = result.getInt(orientationColumnIndex);
                    }

                    if (dataColumnIndex > -1) {
                        String path = result.getString(dataColumnIndex);
                        rotation |= getExifOrientation(path);
                    }
                    return rotation;
                }
            } finally {
                result.close();
            }
        }
    }
    return 0;
}

From source file:im.delight.android.baselib.Social.java

/**
 * Returns a list of phone numbers for the contact with the given lookup ID
 *
 * @param lookupID the lookup ID to get the phone numbers for
 * @param context Context instance to get the ContentResolver from
 * @return CharSequence[] containing all phone numbers for the given contact
 *//* ww  w . j  a  v a2 s .c o m*/
public static CharSequence[] getContactPhone(String lookupID, Context context) {
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
    String where = ContactsContract.Contacts.LOOKUP_KEY + " = ?";
    String[] selectionArgs = new String[] { lookupID };
    String sortOrder = null;
    Cursor result = context.getContentResolver().query(uri, projection, where, selectionArgs, sortOrder);
    String phone;
    if (result != null) {
        if (result.getCount() > 0) {
            CharSequence[] res = new CharSequence[result.getCount()];
            int i = 0;
            while (result.moveToNext()) {
                phone = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                if (phone != null) {
                    res[i] = phone;
                    i++;
                }
            }
            result.close();
            return res;
        } else {
            result.close();
            return null;
        }
    } else {
        return null;
    }
}

From source file:im.delight.android.baselib.Social.java

/**
 * Returns a list of email addresses for the contact with the given lookup ID
 *
 * @param lookupID the lookup ID to get the email addresses for
 * @param context Context instance to get the ContentResolver from
 * @return CharSequence[] containing all email addresses for the given contact
 *///from   www.  j ava  2  s . c om
public static CharSequence[] getContactEmail(String lookupID, Context context) {
    Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Email.DATA };
    String where = ContactsContract.Contacts.LOOKUP_KEY + " = ?";
    String[] selectionArgs = new String[] { lookupID };
    String sortOrder = null;
    Cursor result = context.getContentResolver().query(uri, projection, where, selectionArgs, sortOrder);
    String email;
    if (result != null) {
        if (result.getCount() > 0) {
            CharSequence[] res = new CharSequence[result.getCount()];
            int i = 0;
            while (result.moveToNext()) {
                email = result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                if (email != null) {
                    res[i] = email;
                    i++;
                }
            }
            result.close();
            return res;
        } else {
            result.close();
            return null;
        }
    } else {
        return null;
    }
}

From source file:net.kourlas.voipms_sms.Utils.java

/**
 * Gets a URI pointing to a contact's photo, given the URI for that contact.
 *
 * @param applicationContext The application context.
 * @param uri                The URI of the contact.
 * @return a URI pointing to the contact's photo.
 *//*from   w  w w. j a  v a2  s  .  c  om*/
public static String getContactPhotoUri(Context applicationContext, Uri uri) {
    Cursor cursor = applicationContext.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI },
            null, null, null);
    if (cursor.moveToFirst()) {
        String photoUri = cursor
                .getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
        cursor.close();
        return photoUri;
    } else {
        cursor.close();
        return null;
    }
}

From source file:at.diamonddogs.util.Utils.java

/**
 * Creates a {@link String} {@link List} from a {@link Cursor}
 *
 * @param cursor the input {@link Cursor}
 * @param name   the name of the colum/* w  w w .j a v a 2 s . com*/
 * @return a {@link String} {@link List}
 */
public static List<String> convertColumnToList(Cursor cursor, String name) {
    List<String> list = new ArrayList<>();
    if (!checkCursor(cursor)) {
        return null;
    }
    cursor.moveToFirst();
    do {
        list.add(cursor.getString(cursor.getColumnIndex(name)));
    } while (cursor.moveToNext());

    return list;
}

From source file:Main.java

public static LinkedHashMap<String, Uri> GetMatchedContactsList(Context context, String searchTerm) {
    LinkedHashMap<String, Uri> contactList = new LinkedHashMap<String, Uri>();
    ContentResolver cr = context.getContentResolver();
    String columns[] = { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };
    Cursor cur;
    if (searchTerm == null) {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);
    } else {/* w ww.  jav  a2 s  . c  o  m*/
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns,
                ContactsContract.Contacts.DISPLAY_NAME + " LIKE "
                        + DatabaseUtils.sqlEscapeString("%" + searchTerm + "%"),
                null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
    }
    if (cur != null) {
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String photoURI = cur
                        .getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                if (photoURI != null) {
                    Uri thumbUri = Uri.parse(photoURI);
                    contactList.put(name, thumbUri);
                }
            }
        }
        cur.close();
    }
    return contactList;
}

From source file:org.zywx.wbpalmstar.engine.EUtil.java

public static java.net.Proxy checkJavaProxy(Context context) {
    java.net.Proxy proxy = null;//from   w  ww  . j  a  va  2 s  .c om
    if (!wifiEnable(context)) {// ??APN
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (mCursor != null && mCursor.moveToFirst()) {
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            int proxyPort = mCursor.getInt(mCursor.getColumnIndex("port"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                if (0 == proxyPort) {
                    proxyPort = 80;
                }
                proxy = new java.net.Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyStr, proxyPort));
            }
            mCursor.close();
        }
    }
    return proxy;
}