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:Main.java

protected static long findThreadId(Context context, String messageId) {
    StringBuilder sb = new StringBuilder('(');
    sb.append(Telephony.Mms.MESSAGE_ID);
    sb.append('=');
    sb.append(DatabaseUtils.sqlEscapeString(messageId));
    sb.append(" AND ");
    sb.append(Telephony.Mms.MESSAGE_TYPE);
    sb.append('=');
    sb.append(MESSAGE_TYPE_SEND_REQ);/* www.j  a va  2s. c  o  m*/

    // TODO ContentResolver.query() appends closing ')' to the selection argument
    // sb.append(')');

    Cursor cursor = context.getContentResolver().query(Telephony.Mms.CONTENT_URI,
            new String[] { Telephony.Mms.THREAD_ID }, sb.toString(), null, null);

    if (cursor != null) {
        try {
            if ((cursor.getCount() == 1) && cursor.moveToFirst()) {
                return cursor.getLong(0);
            }
        } finally {
            cursor.close();
        }
    }

    return -1;
}

From source file:Main.java

public static Bundle getContactFromNumber(String number, Context context) {
    ContentResolver cr = context.getContentResolver();
    String[] projection = new String[] { Contacts._ID, Contacts.DISPLAY_NAME };
    Bundle bundle = new Bundle();
    Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(number));
    Cursor cursor = cr.query(contactUri, projection, null, null, null);
    try {/*from  w  w  w.j  a  va2  s.  c o  m*/
        if (cursor.moveToFirst()) {
            for (String key : projection) {
                bundle.putString(key, cursor.getString(cursor.getColumnIndex(key)));
            }
        }
        return bundle;
    } catch (Exception e) {
        return null;
    } finally {
        cursor.close();
    }

}

From source file:Main.java

public static String getRealPathByUri(Context context, Uri uri) {
    if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        return uri.getPath();
    }//from   w  ww .  j  av  a  2 s. c  o  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 columns for table//from   ww  w  .  java2s .co m
 * @param db
 * @param tableName
 * @return
 */
public static List<String> getColumns(SQLiteDatabase db, String tableName) {
    List<String> ar = null;
    Cursor c = null;
    try {
        c = db.rawQuery("select * from " + tableName + " limit 1", null);
        if (c != null) {
            ar = new ArrayList<String>(Arrays.asList(c.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    } finally {
        if (c != null)
            c.close();
    }
    return ar;
}

From source file:fr.mixit.android.utils.SyncUtils.java

public static String getLocalMd5(ContentResolver resolver, String url) {
    final String syncId = MixItContract.Sync.generateSyncId(url);
    final Uri uri = MixItContract.Sync.buildSyncUri(syncId);
    Cursor cursor = resolver.query(uri, SyncQuery.PROJECTION, null, null, null);
    try {/*from  ww  w.j a  v  a2 s .c o m*/
        if (!cursor.moveToFirst())
            return "";
        return cursor.getString(SyncQuery.MD5);
    } finally {
        cursor.close();
    }
}

From source file:Main.java

public static String getRealPathFromURI(Uri contentUri, ContentResolver content_resolver) {
    Cursor cursor = null;
    try {/*w ww.  java2s  .  c o m*/
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = content_resolver.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();
        }
    }
}

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 w  w .j av a2 s. c  om*/
 * @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) {

    final String[] projection = { MediaStore.MediaColumns.DATA };
    final Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);

    if (cursor != null) {
        try {

            if (cursor.moveToFirst()) {
                return cursor.getString(0);
            }
        } finally {
            cursor.close();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets a contact number and then displays the name of contact in the DP
 *
 * @param context       context of the activity from which it was called.
 * @param contactNumber a string representation of the contact number
 * @return returns the number if no contact exist.
 *//*  w  w  w .  ja v  a 2s .co  m*/
public static String getContactName(Context context, String contactNumber) {
    String displayName = null;
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactNumber));

    Cursor cur = context.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

    if (cur != null && cur.moveToFirst()) {
        displayName = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    } else {
        displayName = contactNumber;
    }

    if (!cur.isClosed()) {
        cur.close();
    }

    return displayName;
}

From source file:Main.java

/**
 * Looks up contact name in the address book by the phone number.
 * /*from ww w.j  av a 2 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

private static Set<Integer> getAllAvailableProtocolVersions(Context context) {
    ContentResolver contentResolver = context.getContentResolver();
    Set<Integer> allAvailableVersions = new HashSet<Integer>();
    Uri uri = Uri.parse("content://appsneva.facebook.orca.provider.MessengerPlatformProvider/versions");
    String[] projection = new String[] { "version" };
    Cursor c = contentResolver.query(uri, projection, null, null, null);
    if (c != null) {
        try {//from ww w .  j av  a2 s  . co  m
            int versionColumnIndex = c.getColumnIndex("version");
            while (c.moveToNext()) {
                int version = c.getInt(versionColumnIndex);
                allAvailableVersions.add(version);
            }
        } finally {
            c.close();
        }
    }
    return allAvailableVersions;
}