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

static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };
    try {/*  w  ww  .  j  av  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);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return "";
}

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  a  va 2 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) {

    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 index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } 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. 
 *
 * @param context The context. //  www.jav  a  2  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. 
 */
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 index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

/**
 * Get the contact name from a URI.// www  .jav a 2s .co  m
 *
 * @param context The context.
 * @param contactUri The contact URI.
 *
 * @return The contact name.
 */
public static String getContactName(final Context context, Uri contactUri) {
    String name = null;
    if (contactUri != null) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(contactUri,
                    new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }

        }
    }

    return name;
}

From source file:com.textuality.lifesaver2.Columns.java

public static Map<String, Boolean> loadKeys(Context context, Uri provider, Columns columns) {
    Cursor cursor = context.getContentResolver().query(provider, null, null, null, null);
    Boolean exists = new Boolean(true);
    Map<String, Boolean> map = new Hashtable<String, Boolean>();
    while (cursor.moveToNext()) {
        map.put(columns.cursorToKey(cursor), exists);
    }//from  w  w w.  j  a v  a  2 s  . co m
    cursor.close();
    return map;
}

From source file:Main.java

/**
 * Try to return the absolute file path from the given Uri
 *
 * @param context/* ww w  .j  a v a2  s  .  c om*/
 * @param uri
 * @return the file path or null
 */
public static String uri2FilePath(final Context context, final Uri uri) {
    if (null == uri)
        return null;
    final String scheme = uri.getScheme();
    String data = null;
    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:info.guardianproject.otr.app.im.app.DatabaseUtils.java

public static RoundedAvatarDrawable getAvatarFromAddress(ContentResolver cr, String address, int width,
        int height) throws DecoderException {

    String[] projection = { Imps.Contacts.AVATAR_DATA };
    String[] args = { address };/*www .  j ava 2 s .c om*/
    String query = Imps.Contacts.USERNAME + " LIKE ?";
    Cursor cursor = cr.query(Imps.Contacts.CONTENT_URI, projection, query, args,
            Imps.Contacts.DEFAULT_SORT_ORDER);

    if (cursor.moveToFirst()) {
        String hexData = cursor.getString(0);
        cursor.close();
        if (hexData.equals("NULL")) {
            return null;
        }

        byte[] data = Hex.decodeHex(hexData.substring(2, hexData.length() - 1).toCharArray());

        return decodeAvatar(data, width, height);
    } else {

        cursor.close();
        return null;
    }
}

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

public static int pendingFileTransfersNum() {
    int result = 0;
    Cursor c;

    c = sDB.query(TABLE_DOWNLOADS, new String[] { "1" }, FILTER_DOWNLOAD, null, null, null, null);
    if (c != null) {
        result += c.getCount();//from ww  w  .j  a v  a2  s.co  m
    }
    c.close();

    c = sDB.query(false, TABLE_UPLOADS, new String[] { "1" }, null, null, null, null, null, null);
    if (c != null) {
        result += c.getCount();
    }
    c.close();
    return result;
}

From source file:Main.java

public static String getDataColumn(final Context context, final Uri uri, final String selection,
        final String[] selectionArgs) {
    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {// w  w w. j av a2 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);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:edu.stanford.mobisocial.dungbeetle.ui.fragments.ObjCommentsFragment.java

/**
 * The parametrization here is absolutely not final.
 *//* w  w  w .j a v  a2 s.co  m*/
public static View getViewForObjComments(Activity activity, Uri feedUri, JSONObject obj) {

    Cursor c = activity.getContentResolver().query(feedUri, null, DbObjects.getFeedObjectClause(null), null,
            DbObject._ID + " DESC LIMIT 2");
    try {
        SpinnerAdapter adapter = new ObjectListCursorAdapter(activity, c);

        Gallery gallery = new Gallery(activity);
        gallery.setLayoutParams(CommonLayouts.FULL_SCREEN);
        gallery.setAdapter(adapter);

        return gallery;
    } finally {
        c.close();
    }
}