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

public static String getRealFilePath(final Context context, final Uri uri) {
    if (null == uri)
        return null;
    final String scheme = uri.getScheme();
    String data = null;//  w w w  . ja  va2 s  .co m
    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri,
                new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:Main.java

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {//from  w w  w.  ja  v a 2s .c o  m
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } catch (Exception e) {
        //            FileLog.e("tmessages", e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

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.
 * //from w  ww.jav a  2 s. c  o m
 * @param context
 *            The context.
 * @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) {
    Cursor cursor = null;
    final String column = MediaColumns.DATA;
    final String[] projection = { column };
    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {/*from   w  ww .jav a  2  s . c o  m*/
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } catch (Exception e) {
        Log.d("android_utilities", "the error is " + e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get {@link File} object for the given Android URI.<br>
 * Use content resolver to get real path if direct path doesn't return valid file.
 *//*from  ww  w.ja  v a2s  .c  o  m*/
private static File getFileFromUri(Context context, Uri uri) {

    // first try by direct path
    File file = new File(uri.getPath());
    if (file.exists()) {
        return file;
    }

    // try reading real path from content resolver (gallery images)
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String realPath = cursor.getString(column_index);
            file = new File(realPath);
        }
    } catch (Exception ignored) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return file;
}

From source file:Main.java

public static String getContactName(Context context, String number) {

    String name = null;//from   w ww  .j  av  a 2  s .co m

    // define the columns I want the query to return
    String[] projection = new String[] { Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
            ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
            : ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.PhoneLookup._ID };

    // encode the phone number and build the filter URI
    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

    // query time
    Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
            Log.v("getContactName", "Started uploadcontactphoto: Contact Found @ " + number);
            Log.v("getContactName", "Started uploadcontactphoto: Contact name  = " + name);
        } else {
            Log.v("getContactName", "Contact Not Found @ " + number);
        }
        cursor.close();
    }
    return name;
}

From source file:Main.java

public static boolean hasContact(Context mContext, String contact) {
    ContentResolver cr = mContext.getContentResolver();
    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));
                    if (phoneNo.contains(contact) || contact.contains(phoneNo)) {
                        return true;
                    }/*  www .j  a  v a 2  s.c  o m*/
                }
                pCur.close();
            }
        }
    }
    return false;
}

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.// w ww  .ja v  a 2  s.  c o  m
 * @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.
 */
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:net.ccghe.emocha.model.DBAdapter.java

public static FileInfo getFile(String path) throws SQLException {
    FileInfo info;/*from w w  w  .  j  a v a  2s .  co  m*/
    Cursor c = sDB.query(true, TABLE_DOWNLOADS,
            new String[] { "path", "ts", "size", "md5", "to_delete", "to_download" }, "path='" + path + "'",
            null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    info = new FileInfo(c);
    c.close();
    return info;
}

From source file:Main.java

public static ArrayList<ContentValues> getCity(SQLiteDatabase db, int dqx_dqxx01, boolean municipalities) {
    ArrayList<ContentValues> list = new ArrayList<ContentValues>();
    ContentValues values = null;/* w w  w  .  j av a  2  s .c o  m*/
    Cursor cursor = db.query(TABLE_NAME, new String[] { "DQXX01", "DQXX02" }, "DQX_DQXX01=?",
            new String[] { "" + dqx_dqxx01 }, null, null, "DQXX01 ASC");
    if (cursor != null) {
        if (municipalities) {
            cursor.moveToNext();
        }
        while (cursor.moveToNext()) {
            values = new ContentValues();
            values.put("city_id", cursor.getInt(0));
            values.put("city", cursor.getString(1));
            list.add(values);
        }
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return list;
}