Example usage for android.database Cursor close

List of usage examples for android.database Cursor close

Introduction

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

Prototype

void close();

Source Link

Document

Closes the Cursor, releasing all of its resources and making it completely invalid.

Usage

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

/**
 * Returns a list of email addresses for the contact with the given lookup ID
 *
 * @param contactLookupId a contact's lookup ID to get the email addresses for
 * @param context a context reference//from  w  w w  .  jav a2 s.c  om
 * @return CharSequence[] a list of all email addresses for the given contact or `null`
 */
public static CharSequence[] getContactEmail(final String contactLookupId, final Context context) {
    final Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    final String[] projection = new String[] { ContactsContract.CommonDataKinds.Email.DATA };
    final String where = ContactsContract.Contacts.LOOKUP_KEY + " = ?";
    final String[] selectionArgs = new String[] { contactLookupId };
    final String sortOrder = null;

    Cursor result = context.getContentResolver().query(uri, projection, where, selectionArgs, sortOrder);

    String email;
    if (result != null) {
        if (result.getCount() > 0) {
            final 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:im.delight.android.commons.Social.java

/**
 * Returns a list of phone numbers for the contact with the given lookup ID
 *
 * @param contactLookupId a contact's lookup ID to get the phone numbers for
 * @param context a context reference/*from w w  w. ja  v  a2 s.  c  o  m*/
 * @return CharSequence[] a list of all phone numbers for the given contact or `null`
 */
public static CharSequence[] getContactPhone(final String contactLookupId, final Context context) {
    final Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    final String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
    final String where = ContactsContract.Contacts.LOOKUP_KEY + " = ?";
    final String[] selectionArgs = new String[] { contactLookupId };
    final String sortOrder = null;

    Cursor result = context.getContentResolver().query(uri, projection, where, selectionArgs, sortOrder);

    String phone;
    if (result != null) {
        if (result.getCount() > 0) {
            final 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:ro.weednet.contactssync.platform.ContactManager.java

public static int getPhotoPickSize(Context context) {
    Cursor c = context.getContentResolver().query(DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
            new String[] { DisplayPhoto.DISPLAY_MAX_DIM }, null, null, null);

    try {/*w  w  w. j  a  v a 2s. c o m*/
        c.moveToFirst();
        return c.getInt(0);
    } catch (Exception e) {
    } finally {
        if (c != null) {
            c.close();
        }
    }

    return ContactsSync.getInstance().getPictureSize();
}

From source file:Main.java

/**
 * Retourner le nom de calendrier//from   w w  w. j av a  2  s.  com
 * 
 * @param id
 * @return
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static String getCalendarName(ContentResolver contentResolver, String id) {
    String name = null;
    Cursor cursor;
    String[] projection;
    Uri calendarUri;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        calendarUri = CalendarContract.Calendars.CONTENT_URI;
        projection = new String[] { CalendarContract.Calendars._ID,
                CalendarContract.Calendars.CALENDAR_DISPLAY_NAME };
    } else if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
        calendarUri = Uri.parse("content://com.android.calendar/calendars");
        projection = new String[] { "_id", "displayname" };
    } else {
        calendarUri = Uri.parse("content://calendar/calendars");
        projection = new String[] { "_id", "displayname" };
    }

    cursor = contentResolver.query(calendarUri, projection, "_id = ?", new String[] { id }, null);

    if (cursor.moveToFirst()) {
        name = cursor.getString(1);
    }

    cursor.close();

    return name;
}

From source file:com.nononsenseapps.notepad.sync.googleapi.GoogleTaskSync.java

static TaskList loadRemoteListFromDB(final Context context, final GoogleTaskList remoteList) {
    if (remoteList.dbid == null || remoteList.dbid < 1)
        return null;

    final Cursor c = context.getContentResolver().query(TaskList.getUri(remoteList.dbid),
            TaskList.Columns.FIELDS, null, null, null);
    TaskList tl = null;//w w w  .  j  a va2  s  .com
    try {
        if (c.moveToFirst()) {
            tl = new TaskList(c);
        }
    } finally {
        if (c != null)
            c.close();
    }

    return tl;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static String getStringField(Uri uri, String columnName) {
    String result = null;//ww  w . j  a va  2 s  . c  o m
    final String[] projection = new String[] { columnName };
    final Cursor c = sResolver.query(uri, projection, null, null, null);
    try {
        if (c.moveToFirst()) {
            result = c.getString(c.getColumnIndex(columnName));
        }
    } finally {
        c.close();
    }
    return result;
}

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 w  w w  .  j  a  v  a 2s. c o  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:ro.weednet.contactssync.platform.ContactManager.java

private static long lookupProfile(ContentResolver resolver, String userId) {
    long profileId = 0;
    final Cursor c = resolver.query(Data.CONTENT_URI, ProfileQuery.PROJECTION, ProfileQuery.SELECTION,
            new String[] { userId }, null);
    try {//from www. j  ava 2s.c  om
        if ((c != null) && c.moveToFirst()) {
            profileId = c.getLong(ProfileQuery.COLUMN_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return profileId;
}

From source file:ro.weednet.contactssync.platform.ContactManager.java

public static List<RawContact> getStarredContacts(Context context, Uri uri) {
    Log.i(TAG, "*** Looking for starred contacts");

    final ContentResolver resolver = context.getContentResolver();

    Set<Long> contactIds = new HashSet<Long>();
    Cursor c = resolver.query(RawContacts.CONTENT_URI, new String[] { RawContacts.CONTACT_ID },
            RawContacts.STARRED + "!=0", null, null);
    try {/*from  w  ww .jav  a2s . c  o  m*/
        while (c.moveToNext()) {
            contactIds.add(c.getLong(0));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    Log.i(TAG, "*** ... found " + contactIds.size() + " starred");

    int i = 0;
    StringBuilder sb = new StringBuilder();
    for (Long s : contactIds) {
        sb.append(s);
        if (++i >= Math.min(contactIds.size(), 50)) {
            break;
        }
        sb.append(",");
    }

    List<RawContact> contacts = new ArrayList<RawContact>();
    c = resolver.query(uri, new String[] { Contacts._ID, RawContacts.SOURCE_ID },
            RawContacts.CONTACT_ID + " IN (" + sb.toString() + ")", null, null);
    try {
        while (c.moveToNext()) {
            final long rawContactId = c.getLong(0);
            final String serverContactId = c.getString(1);
            RawContact rawContact = RawContact.create(rawContactId, serverContactId);
            contacts.add(rawContact);
        }
    } catch (Exception e) {
        Log.i(TAG, "failing .. " + e.toString());
    } finally {
        if (c != null) {
            c.close();
        }
    }

    Log.i(TAG, "*** ... and " + contacts.size() + " of mine " + sb.toString());
    return contacts;
}

From source file:com.popcorntime.apps.remote.utils.Utils.java

public static String getRealPathFromUri(Context context, Uri contentUri) {
    Log.i("uri", contentUri.toString());
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        Cursor cursor = null;
        try {/*from www  .j  av  a2s .c  o m*/

            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else {
        Uri uri = contentUri;
        // DocumentProvider
        if (DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }

                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri2 = ContentUris
                        .withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(context, contentUri2, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri2 = null;
                if ("image".equals(type)) {
                    contentUri2 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    //contentUri2 = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    //contentUri2 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[] { split[1] };

                return getDataColumn(context, contentUri2, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }
}