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:ca.mudar.parkcatcher.utils.ParserUtils.java

/**
 * Query and return the newest {@link SyncColumns#UPDATED} time for all
 * entries under the requested {@link Uri}. Expects the {@link Uri} to
 * reference a directory of several items.
 *//*from  w  w  w. j  av  a  2 s  .  co m*/
public static long queryDirUpdated(Uri uri, ContentResolver resolver) {
    final String[] projection = { "MAX(" + SyncColumns.UPDATED + ")" };
    final Cursor cursor = resolver.query(uri, projection, null, null, null);
    try {
        cursor.moveToFirst();
        return cursor.getLong(0);
    } finally {
        cursor.close();
    }
}

From source file:Main.java

/**
 * @param context The {@link Context} to use.
 * @param id The id of the album.//from  w  w w . ja  va2  s .  com
 * @return The release date for an album.
 */
public static final String getReleaseDateForAlbum(final Context context, final long id) {
    if (id == -1) {
        return null;
    }
    Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, id);
    Cursor cursor = context.getContentResolver().query(uri, new String[] { AlbumColumns.FIRST_YEAR }, null,
            null, null);
    String releaseDate = null;
    if (cursor != null) {
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            releaseDate = cursor.getString(0);
        }
        cursor.close();
        cursor = null;
    }
    return releaseDate;
}

From source file:Main.java

/**
 * @param context The {@link Context} to use.
 * @param id The id of the album.//from   www .j a v  a  2s  .c om
 * @return The song count for an album.
 */
public static final String getSongCountForAlbum(final Context context, final long id) {
    if (id == -1) {
        return null;
    }
    Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, id);
    Cursor cursor = context.getContentResolver().query(uri, new String[] { AlbumColumns.NUMBER_OF_SONGS }, null,
            null, null);
    String songCount = null;
    if (cursor != null) {
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            songCount = cursor.getString(0);
        }
        cursor.close();
        cursor = null;
    }
    return songCount;
}

From source file:Main.java

public static String getFileName(Uri uri, Context context) {
    String result = null;//from w  ww . j  a v a 2  s .  c om
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

From source file:Main.java

/**
 * Handles V19 and up uri's/*from  w  ww  .  ja  v  a2 s .  c  om*/
 * @param context
 * @param contentUri
 * @return path
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getPathForV19AndUp(Context context, Uri contentUri) {
    String wholeID = DocumentsContract.getDocumentId(contentUri);

    // Split at colon, use second item in the array
    String id = wholeID.split(":")[1];
    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String sel = MediaStore.Images.Media._ID + "=?";
    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            sel, new String[] { id }, null);

    String filePath = "";
    int columnIndex = cursor.getColumnIndex(column[0]);
    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }

    cursor.close();
    return filePath;
}

From source file:Main.java

public static String getRealPathFromURI(Uri contentUri, Context ctx) {
    Log.d("thong", "Uri: " + contentUri.toString());

    try {/*from www . j a  v a 2s .c om*/
        String realpath = "";

        String[] proj = { MediaStore.Images.Media.DATA };

        //Cursor cursor = ((Activity) ctx).managedQuery(contentUri, proj, null, null, null);

        Cursor cursor = ctx.getContentResolver().query(contentUri, proj, null, null, null);

        Log.d("thong", "Column count: " + cursor.getColumnCount());

        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();

        realpath = cursor.getString(column_index);

        cursor.close();

        return realpath;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static List<String> getColumns(SQLiteDatabase db, String tableName) {
    List<String> columns = new ArrayList<>();
    Cursor cursor = null;
    try {//from  w ww .j  a  v  a  2 s.c  o m
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
        if (cursor != null) {
            columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return columns;
}

From source file:Main.java

public static Map<String, Integer> getProvince(SQLiteDatabase db, String tableName) {
    Map<String, Integer> provinceMap = new LinkedHashMap<String, Integer>();
    Cursor cursor = db.query(tableName, new String[] { "DQX_DQXX01", "DQXX02" }, "DQXX03=?",
            new String[] { "1" }, null, null, "DQX_DQXX01 ASC");
    if (cursor != null) {
        while (cursor.moveToNext()) {
            provinceMap.put(cursor.getString(1), cursor.getInt(0));
        }//w w w  .j a  va  2 s  . c  o m
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return provinceMap;
}

From source file:Main.java

public static boolean isMediaScannerScanning(Context context) {
    boolean result = false;
    Cursor cursor = query(context, MediaStore.getMediaScannerUri(),
            new String[] { MediaStore.MEDIA_SCANNER_VOLUME }, null, null, null);
    if (cursor != null) {
        if (cursor.getCount() == 1) {
            cursor.moveToFirst();/*from w ww .  ja v a 2 s .  c  o  m*/
            result = "external".equals(cursor.getString(0));
        }
        cursor.close();
    }

    return result;
}

From source file:Main.java

private static List<String> getColumns(SQLiteDatabase db, String tableName) {
    List<String> columns = new ArrayList<String>();
    Cursor cursor = null;
    try {//from   w  w  w  . ja v a  2s.c  om
        cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null);
        if (cursor != null) {
            columns = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return columns;
}