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 getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {/*from  w w  w.jav  a2 s .  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);
    } catch (NullPointerException ex) {
        return "";
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

public static LinkedList<Integer> retrieveIntegerFromCursor(Cursor cursor, String columnName) {
    if (null == cursor || 0 == cursor.getCount()) {
        return new LinkedList<Integer>();
    }/*from   w w w . j  av a2  s.  c om*/

    LinkedList<Integer> ids = new LinkedList<Integer>();

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndexOrThrow(columnName));
            ids.add(new Integer(id));
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return ids;
}

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 www.  j  a  v a 2  s . com
 * @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 = MediaStore.MediaColumns.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:Main.java

public static File uri2File(Activity context, Uri uri) {
    File file;//from   w w w .  j a  va 2  s.c o m
    String[] project = { MediaStore.Images.Media.DATA };
    Cursor actualImageCursor = context.getContentResolver().query(uri, project, null, null, null);
    if (actualImageCursor != null) {
        int actual_image_column_index = actualImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualImageCursor.moveToFirst();
        String img_path = actualImageCursor.getString(actual_image_column_index);
        file = new File(img_path);
    } else {
        file = new File(uri.getPath());
    }
    if (actualImageCursor != null)
        actualImageCursor.close();
    return file;
}

From source file:m2.android.archetype.example.FacebookSdk.Settings.java

public static String getAttributionId(ContentResolver contentResolver) {
    String[] projection = { ATTRIBUTION_ID_COLUMN_NAME };
    Cursor c = contentResolver.query(ATTRIBUTION_ID_CONTENT_URI, projection, null, null, null);
    if (c == null || !c.moveToFirst()) {
        return null;
    }//w  w w.  j a  va2  s .  com
    String attributionId = c.getString(c.getColumnIndex(ATTRIBUTION_ID_COLUMN_NAME));
    c.close();
    return attributionId;
}

From source file:cn.edu.mju.Thriphoto.net.NetStateManager.java

/**
 * ?? APN  {@link org.apache.http.HttpHost} 
 *
 * @return {@link org.apache.http.HttpHost} 
 *///  w  w w  .java2s.com
public static HttpHost getAPN() {
    HttpHost proxy = null;
    Uri uri = Uri.parse("content://telephony/carriers/preferapn");
    Cursor mCursor = null;
    if (null != mContext) {
        mCursor = mContext.getContentResolver().query(uri, null, null, null, null);
    }
    if (mCursor != null && mCursor.moveToFirst()) {
        // ???
        String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
        if (proxyStr != null && proxyStr.trim().length() > 0) {
            proxy = new HttpHost(proxyStr, 80);
        }
        mCursor.close();
    }
    return proxy;
}

From source file:com.cyanogenmod.filemanager.util.MediaHelper.java

/**
 * Method that converts a content uri to a file system path
 *
 * @param cr The content resolver/*from  w ww.j  a  va 2s  .c  om*/
 * @param id The media database id
 * @param volume The volume
 * @return File The file reference
 */
private static File mediaIdToFile(ContentResolver cr, long id, String volume) {
    final String[] projection = { MediaColumns.DATA };
    final String where = MediaColumns._ID + " = ?";
    Uri baseUri = MediaStore.Files.getContentUri(volume);
    Cursor c = cr.query(baseUri, projection, where, new String[] { String.valueOf(id) }, null);
    try {
        if (c != null && c.moveToNext()) {
            return new File(c.getString(c.getColumnIndexOrThrow(MediaColumns.DATA)));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return null;
}

From source file:net.lamp.support.NetStateManager.java

/**
 * ?? APN ?{@link HttpHost} /*from   w  ww.j ava  2  s .c o m*/
 * 
 * @return {@link HttpHost} 
 */
public static HttpHost getAPN() {
    HttpHost proxy = null;
    Uri uri = Uri.parse("content://telephony/carriers/preferapn");
    Cursor mCursor = null;
    if (null != mContext) {
        mCursor = mContext.getContentResolver().query(uri, null, null, null, null);
    }
    if (mCursor != null && mCursor.moveToFirst()) {
        // ????  
        String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
        if (proxyStr != null && proxyStr.trim().length() > 0) {
            proxy = new HttpHost(proxyStr, 80);
        }
        mCursor.close();
    }
    return proxy;
}

From source file:com.fsck.k9.helper.Utility.java

/**
 * Unconditionally close a Cursor.  Equivalent to {@link Cursor#close()},
 * if cursor is non-null.  This is typically used in finally blocks.
 *
 * @param cursor cursor to close/* w w w  . ja  v a 2 s. c o  m*/
 */
public static void closeQuietly(final Cursor cursor) {
    if (cursor != null) {
        cursor.close();
    }
}

From source file:Main.java

public static String getPersonNameFromNumber(Context context, String box, String address) {
    if (address == null) {
        return "unknown";
    }/*www  .  ja va 2s.co  m*/
    if (!box.equalsIgnoreCase("draft")) {
        Cursor cursor = context.getContentResolver().query(
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address)),
                new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.getCount() > 0) {
                    cursor.moveToFirst();
                    String name = cursor.getString(0);
                    return name;
                }
            } finally {
                cursor.close();
            }
        }
    }
    if (address != null) {
        return PhoneNumberUtils.formatNumber(address);
    }
    return "unknown";
}