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, long album_id) {
    Cursor cursor = null;
    try {/*from  ww  w .  j a v  a 2s. c o m*/
        cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Audio.Albums.ALBUM_ART }, MediaStore.Audio.Albums._ID + "=?",
                new String[] { String.valueOf(album_id) }, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

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 {/*w  w w  .j  a v  a2s.  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:Main.java

public static int getOrientation(Context context, Uri uri) {
    Cursor cursor = null;
    try {//  w w  w.ja v  a  2 s.  co  m
        cursor = context.getContentResolver().query(uri,
                new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
        if (cursor == null) {
            return 0;
        }
        if (cursor.moveToNext()) {
            int ori = cursor.getInt(0);
            return ori;
        } else {
            return -1;
        }
    } catch (SQLiteException e) {
        return 0;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

public static String getMessageCount(Context context, String id) {
    String res = null;/*from  w  w w  .ja v  a 2 s  . com*/
    try {
        final String[] projection = new String[] { "_id", "message_count" };
        Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
        Cursor query = context.getContentResolver().query(uri, projection, null, null, "date DESC");
        if (query != null) {
            boolean find = false;
            while (query.moveToNext() && !find) {
                if (query.getString(query.getColumnIndex("_id")).equals(id)) {
                    res = query.getString(query.getColumnIndex("message_count"));
                    //                        Log.v("getMessageCount", "find, nb_sms = "+res);
                    find = true;
                }
            }
            query.close();
        }
    } catch (Exception e) {
        //            Log.v("getMessageCount", "Erreur");
        e.printStackTrace();
    }
    return res;
}

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  w w w.  j a  v  a 2s .  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:com.goliathonline.android.kegbot.io.RemoteDrinksHandler.java

private static ContentValues queryDrinkDetails(Uri uri, ContentResolver resolver) {
    final ContentValues values = new ContentValues();
    final Cursor cursor = resolver.query(uri, SessionsQuery.PROJECTION, null, null, null);
    try {//from  w  w  w .  jav a 2  s .c  o  m
        if (cursor.moveToFirst()) {
            values.put(SyncColumns.UPDATED, cursor.getLong(SessionsQuery.UPDATED));
            values.put(Drinks.DRINK_STARRED, cursor.getInt(SessionsQuery.STARRED));
        } else {
            values.put(SyncColumns.UPDATED, KegbotContract.UPDATED_NEVER);
        }
    } finally {
        cursor.close();
    }
    return values;
}

From source file:com.contentful.vault.SqliteHelper.java

static void deleteTables(SQLiteDatabase db) {
    String[] columns = new String[] { "name" };
    String selection = "type = ? AND name != ?";
    String[] args = new String[] { "table", "android_metadata" };
    Cursor cursor = db.query("sqlite_master", columns, selection, args, null, null, null);
    List<String> tables = null;
    try {/*from w ww .j ava 2  s  .  c o  m*/
        if (cursor.moveToFirst()) {
            tables = new ArrayList<>();
            do {
                tables.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
    if (tables != null) {
        db.beginTransaction();
        try {
            for (String table : tables) {
                db.execSQL("DROP TABLE " + escape(table));
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
}

From source file:Main.java

public static ArrayList<ContentValues> getArea(SQLiteDatabase db, int dqx_dqxx01) {
    ArrayList<ContentValues> list = new ArrayList<ContentValues>();
    ContentValues values = null;// www .  j a v  a 2 s.  co  m
    Cursor cursor = db.query(TABLE_NAME, new String[] { "DQXX01", "DQXX02", "DQXX05" }, "DQX_DQXX01=?",
            new String[] { "" + dqx_dqxx01 }, null, null, "DQXX01 ASC");
    if (cursor != null) {
        while (cursor.moveToNext()) {
            values = new ContentValues();
            values.put("area_id", cursor.getInt(0));
            values.put("area", cursor.getString(1));
            values.put("full_area", cursor.getString(2));
            list.add(values);
        }
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return list;
}

From source file:ee.ioc.phon.android.speak.Utils.java

/**
 * TODO: should we immediately return null if id = 0?
 *///  w ww.java2s . com
public static String idToValue(Context context, Uri contentUri, String columnId, String columnUrl, long id) {
    String value = null;
    Cursor c = context.getContentResolver().query(contentUri, new String[] { columnUrl }, columnId + "= ?",
            new String[] { String.valueOf(id) }, null);

    if (c.moveToFirst()) {
        value = c.getString(0);
    }
    c.close();
    return value;
}

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 w  w w.j  a  v  a  2s  . co  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);
        }
    } catch (Exception e) {
        Log.e("tmessages", e.getMessage());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}