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:at.diamonddogs.util.Utils.java

/**
 * Checks a cursor for validity//from  w  w w . j av  a 2  s  .c om
 *
 * @param c the {@link Cursor} to check
 * @return <code>true</code> if the cursor is not <code>null</code>, not
 * closed and not empty, <code>false</code> otherwise
 */
public static boolean checkCursor(Cursor c) {
    if (c == null || c.isClosed()) {
        return false;
    }

    if (c.getCount() <= 0) {
        c.close();
        return false;
    }
    return true;
}

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 ww w.ja  va  2  s  . co m
        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

/**
 * @param context The {@link Context} to use.
 * @param name The name of the new playlist.
 * @return A new playlist ID./*from  w ww.  j  a  v a 2  s  .  co m*/
 */
public static final long createPlaylist(final Context context, final String name) {
    if (name != null && name.length() > 0) {
        final ContentResolver resolver = context.getContentResolver();
        final String[] projection = new String[] { PlaylistsColumns.NAME };
        final String selection = PlaylistsColumns.NAME + " = '" + name + "'";
        Cursor cursor = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, projection, selection,
                null, null);
        if (cursor.getCount() <= 0) {
            final ContentValues values = new ContentValues(1);
            values.put(PlaylistsColumns.NAME, name);
            final Uri uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values);
            return Long.parseLong(uri.getLastPathSegment());
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        return -1;
    }
    return -1;
}

From source file:com.goliathonline.android.kegbot.io.RemoteKegHandler.java

private static ContentValues queryKegDetails(Uri uri, ContentResolver resolver) {
    final ContentValues values = new ContentValues();
    final Cursor cursor = resolver.query(uri, KegsQuery.PROJECTION, null, null, null);
    try {/*from w ww  .  j av a  2  s. co  m*/
        if (cursor.moveToFirst()) {
            values.put(SyncColumns.UPDATED, cursor.getLong(KegsQuery.UPDATED));
            values.put(Kegs.KEG_STARRED, cursor.getInt(KegsQuery.STARRED));
        } else {
            values.put(SyncColumns.UPDATED, KegbotContract.UPDATED_NEVER);
        }
    } finally {
        cursor.close();
    }
    return values;
}

From source file:Main.java

public static void DeleteImageFromGalleryBase(final Context context, String fileName) {

    // Set up the projection (we only need the ID)
    String[] projection = { MediaStore.Images.Media._ID };

    // Match on the file path
    String selection = MediaStore.Images.Media.DATA + " = ?";
    String[] selectionArgs = new String[] { fileName };

    // Query for the ID of the media matching the file path
    Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    ContentResolver contentResolver = context.getContentResolver();
    Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
    if (c.moveToFirst()) {
        // We found the ID. Deleting the item via the content provider will also remove the file
        long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
        Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
        contentResolver.delete(deleteUri, null, null);
    } else {/*from  w  w  w.  j a  v a  2  s  . c  o  m*/
        // File not found in media store DB
    }
    c.close();
}

From source file:Main.java

public static long getLastProgramEndTimeMillis(ContentResolver resolver, Uri channelUri) {
    Uri uri = TvContract.buildProgramsUriForChannel(channelUri);
    String[] projection = { Programs.COLUMN_END_TIME_UTC_MILLIS };
    Cursor cursor = null;
    try {//from  w  w w . ja  va  2 s .co  m
        // TvProvider returns programs chronological order by default.
        cursor = resolver.query(uri, projection, null, null, null);
        if (cursor == null || cursor.getCount() == 0) {
            return 0;
        }
        cursor.moveToLast();
        return cursor.getLong(0);
    } catch (Exception e) {
        Log.w(TAG, "Unable to get last program end time for " + channelUri, e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return 0;
}

From source file:com.adjust.sdk.Util.java

protected static String getAttributionId(final Context context) {
    try {/*from   w w  w. j  av  a2 s. c om*/
        final ContentResolver contentResolver = context.getContentResolver();
        final Uri uri = Uri.parse("content://com.facebook.katana.provider.AttributionIdProvider");
        final String columnName = "aid";
        final String[] projection = { columnName };
        final Cursor cursor = contentResolver.query(uri, projection, null, null, null);

        if (null == cursor) {
            return null;
        }
        if (!cursor.moveToFirst()) {
            cursor.close();
            return null;
        }

        final String attributionId = cursor.getString(cursor.getColumnIndex(columnName));
        cursor.close();
        return attributionId;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.example.igorklimov.popularmoviesdemo.helpers.Utility.java

public static int getId(Context context) {
    Uri uri = getContentUri(context);/*from   w  ww .  java  2  s. c o m*/
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
    int id = 0;
    if (cursor != null && cursor.moveToFirst()) {
        id = cursor.getInt(0);
        cursor.close();
    }
    return id;
}

From source file:jog.my.memory.gcm.ServerUtilities.java

/**
 * Gets the real path of the URI returned from the camera
 * @param contentUri - apparent URI of resource
 * @return - actual URI of resource// w  w  w.j  ava 2 s. com
 */
public static String getRealPathFromURI(Uri contentUri, Context context) {
    String[] proj = new String[] { MediaStore.Images.ImageColumns.DATA };

    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();

    String filename = cursor.getString(column_index);
    cursor.close();

    return filename;
}

From source file:Main.java

private static ArrayList<String> getNumbers(Context context) {
    ArrayList<String> list = new ArrayList<String>();
    Cursor cursor = null;
    try {//from  w  w w .  jav a 2  s .co m
        cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
        int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
        cursor.moveToFirst();
        do {
            String phoneNumber = cursor.getString(phoneNumberIdx);
            list.add(phoneNumber);
        } while (cursor.moveToNext());
    } catch (Exception e) {
        Log.d(context.getPackageName(), e.toString());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return list;
}