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

/**
 * 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

private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    String column = MediaStore.Images.Media.DATA;
    String[] projection = { column };
    try {//from   w  w w .  j a va 2  s  .  c  om
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null && !cursor.isClosed())
            cursor.close();
    }
    return null;
}

From source file:Main.java

/**
 * Return file display name from Uri// w  w w . jav a 2 s  . c  o  m
 *
 * @param context Context
 * @param uri URI of the file
 * @return return file display name
 */
public static String getFileDisplayNameFromUri(Context context, Uri uri) {
    String displayName = "";
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
                displayName = cursor.getString(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        displayName = file.getName();
    }
    return displayName;
}

From source file:net.ccghe.emocha.model.DBAdapter.java

private static String getFirst(String column, String filter, String table, int columnId) {
    String result = null;//from www  .ja  v  a  2 s .co m
    Cursor c = sDB.query(table, new String[] { column }, filter, null, null, null, null);
    if (c.getCount() > 0) {
        c.moveToFirst();
        result = c.getString(columnId);
    }
    c.close();
    return result;
}

From source file:Main.java

public static synchronized void deleteCalllogByPhone(Context context, String number) {
    if (TextUtils.isEmpty(number))
        return;//from   w  w w .  j ava 2 s.c o  m
    try {
        Cursor cursor = context.getContentResolver().query(Calls.CONTENT_URI, null, Calls.NUMBER + "=?",
                new String[] { number }, Calls.DATE + " desc");
        if (cursor != null) {
            if (cursor.moveToNext()) {
                long _id = cursor.getLong(cursor.getColumnIndex(Calls._ID));
                context.getContentResolver().delete(Calls.CONTENT_URI, Calls._ID + "=?",
                        new String[] { String.valueOf(_id) });
            }
            cursor.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static ArrayList<ContentValues> getProvince(SQLiteDatabase db) {
    ArrayList<ContentValues> list = new ArrayList<ContentValues>();
    ContentValues values = null;/*w  ww  .j av a 2 s . co m*/
    Cursor cursor = db.query(TABLE_NAME, new String[] { "DQXX01", "DQXX02" }, "DQXX03=?", new String[] { "1" },
            null, null, "DQX_DQXX01 ASC");
    if (cursor != null) {
        while (cursor.moveToNext()) {
            values = new ContentValues();
            values.put("province_id", cursor.getInt(0));
            values.put("province", cursor.getString(1));
            list.add(values);
        }
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return list;
}

From source file:Main.java

public static LinkedList<String> retrieveStringFromCursor(Cursor cursor, String columnName) {
    LinkedList<String> result = new LinkedList<String>();

    if (null == cursor || 0 == cursor.getCount()) {
        return result;
    }//w ww . j  a va 2 s. com

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            String str = cursor.getString(cursor.getColumnIndexOrThrow(columnName));
            result.add(str);
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return result;
}

From source file:Main.java

public static String getRealPathFromURI(Context context, Uri contentURI) {
    String TAG = "PINGUINO-getRealPathFromURI";
    String result;/*  w w w .j  av  a  2  s .  co  m*/
    Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) {
        result = contentURI.getPath();
    } else {
        Log.d(TAG, "cursor1:" + cursor);
        cursor.moveToFirst();
        Log.d(TAG, "cursor2:" + cursor);
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        Log.d(TAG, "idx:" + idx);
        result = cursor.getString(idx);
        Log.d(TAG, "result:" + result);
        cursor.close();
    }
    return result;
}

From source file:de.escoand.readdaily.DownloadHandler.java

public static void stopDownload(final Context context, final String name) {
    Database db = Database.getInstance(context);
    Cursor c = db.getDownloads();
    long id = 0;// w ww. ja  v a  2 s.com

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

    // stop download
    ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).remove(id);
    db.removeDownload(id);
}

From source file:Main.java

public static String getPathFromUri(Context context, Uri uri) {
    if (uri == null) {
        return null;
    }//from   w w  w .ja  va 2s  . c  o m
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor == null) {
            return null;
        }
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}