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:de.escoand.readdaily.DownloadHandler.java

public static float downloadProgress(final Context context, final String name) {
    Cursor cursor = Database.getInstance(context).getDownloads();
    long id = 0;/*  ww w.  j  a  va2s .  c om*/
    float progress;

    // get download id
    while (cursor.moveToNext())
        if (cursor.getString(cursor.getColumnIndex(Database.COLUMN_SUBSCRIPTION)).equals(name)) {
            id = cursor.getLong(cursor.getColumnIndex(Database.COLUMN_ID));
            break;
        }
    cursor.close();
    if (id <= 0)
        return SUBSCRIPTION_DOWNLOAD_UNKNOWN;

    // get download
    cursor = ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE))
            .query(new DownloadManager.Query().setFilterById(id));
    if (!cursor.moveToFirst()) {
        Database.getInstance(context).removeDownload(id);
        return DOWNLOAD_ID_UNKNOWN;
    }

    // get progress
    progress = cursor.getFloat(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
            / cursor.getFloat(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

    cursor.close();
    return progress;
}

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  v a 2s .  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 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 int getStatus(Context context) {
    Cursor query = null;
    String packageName = context.getPackageName();
    try {//from  ww w .  j av a2s  .  c  o  m
        query = context.getContentResolver().query(Uri.parse("content://com.lbe.security.miui.permmgr/active"),
                null, "pkgName=?", new String[] { packageName }, null);
        if (query == null) {
            return 0;
        }
        if (query.moveToFirst()) {
            int status = query.getInt(query.getColumnIndex("userAccept"));
            if (query == null) {
                return status;
            }
        }
        query.close();
    } catch (Exception e) {

        return -1;
    }

    return -1;
}

From source file:net.peterkuterna.android.apps.devoxxsched.io.RemoteSessionsHandler.java

private static int isStarred(Uri uri, ContentResolver resolver) {
    final Cursor cursor = resolver.query(uri, SessionsQuery.PROJECTION, null, null, null);
    int starred = 0;
    try {//from ww  w  . ja  v  a2  s  .  co  m
        if (cursor.moveToFirst()) {
            starred = cursor.getInt(SessionsQuery.STARRED);
        }
    } finally {
        cursor.close();
    }
    return starred;
}

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.// ww w.  ja  va 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.
 * @author paulburke
 */
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()) {
            if (DEBUG)
                DatabaseUtils.dumpCursor(cursor);

            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

/**
 * 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 ww . j a v  a 2  s .co 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.
 */
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 column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_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.//from   w  w w .  j a  va  2  s .com
 * @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 = MediaStore.Images.Media.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);
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:com.trk.aboutme.facebook.Settings.java

/**
 * Acquire the current attribution id from the facebook app.
 * @return returns null if the facebook app is not present on the phone.
 *///w w  w  .  jav a  2 s  .c o  m
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;
    }
    String attributionId = c.getString(c.getColumnIndex(ATTRIBUTION_ID_COLUMN_NAME));
    c.close();
    return attributionId;
}

From source file:mobisocial.noteshere.util.UriImage.java

public static float rotationForImage(Context context, Uri uri) {
    if (uri.getScheme().equals("content")) {
        String[] projection = { Images.ImageColumns.ORIENTATION };
        Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        try {//from   w w w.  j  a  va  2  s .  co  m
            if (c.moveToFirst()) {
                return c.getInt(0);
            }
        } finally {
            c.close();
        }
    } else if (uri.getScheme().equals("file")) {
        try {
            ExifInterface exif = new ExifInterface(uri.getPath());
            int rotation = (int) exifOrientationToDegrees(
                    exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
            return rotation;
        } catch (IOException e) {
            Log.e(TAG, "Error checking exif", e);
        }
    }
    return 0f;
}

From source file:com.ppshein.PlanetMyanmarDictionary.common.java

public static void addBookmark(String notedword, Context context) {
    try {//from  www .  jav a2s.  c  o  m

        DatabaseUtil dbUtil = new DatabaseUtil(context);

        dbUtil.open();
        Cursor cursor = dbUtil.fetchBookmark(notedword);
        String getReturn = cursor.getString(1);
        cursor.close();
        dbUtil.close();
        Log.v("Existing Bookmarks", getReturn);
    } catch (Exception e) {
        DatabaseUtil dbUtil = new DatabaseUtil(context);
        dbUtil.open();
        dbUtil.insertBookmark(notedword);
        dbUtil.close();
        Toast toast = Toast.makeText(context, "Successfully bookmarked", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}