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  ww  .j  a  v a2  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.
 * @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()) {

            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } catch (Exception e) {
        return null;
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

private static Bitmap rotateMediaImage(ContentResolver contentResolver, Uri uri, Bitmap image)
        throws FileNotFoundException {

    Cursor c = contentResolver.query(uri, null, null, null, null);
    try {/*  w w  w.ja  v a2  s .  com*/
        if (c != null) {
            if (c.moveToFirst()) {

                int index = c.getColumnIndex(ImageColumns.ORIENTATION);
                int degrees = index < 0 ? 0 : c.getInt(index);

                if (degrees != 0) {
                    return rotate(image, degrees);
                }
                return image;
            }
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return image;
}

From source file:com.deliciousdroid.platform.ContactManager.java

/**
 * Returns the RawContact id for a sample SyncAdapter contact, or 0 if the
 * sample SyncAdapter user isn't found.//w  ww  . java  2  s.co  m
 * 
 * @param context the Authenticator Activity context
 * @param userId the sample SyncAdapter user ID to lookup
 * @return the RawContact id, or 0 if not found
 */
private static long lookupRawContact(ContentResolver resolver, String userName) {
    long authorId = 0;
    final Cursor c = resolver.query(RawContacts.CONTENT_URI, UserIdQuery.PROJECTION, UserIdQuery.SELECTION,
            new String[] { userName }, null);
    try {
        if (c.moveToFirst()) {
            authorId = c.getLong(UserIdQuery.COLUMN_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return authorId;
}

From source file:Main.java

public static ArrayList<HashMap<String, String>> cursorToHashMap(Cursor cursor) {

    if (cursor != null) {
        int cursorCount = cursor.getCount();
        int columnCount;
        ArrayList<HashMap<String, String>> cursorData = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> rowHashMap;
        for (int i = 0; i < cursorCount; i++) {
            cursor.moveToPosition(i);/*from   w w w.  j a  va 2s . com*/
            rowHashMap = new HashMap<String, String>();
            columnCount = cursor.getColumnCount();
            for (int j = 0; j < columnCount; j++) {
                rowHashMap.put(cursor.getColumnName(j), cursor.getString(j));
            }
            cursorData.add(rowHashMap);
        }
        cursor.close();

        return cursorData;
    } else {
        return null;
    }
}

From source file:com.nbos.phonebook.sync.platform.ContactManager.java

/**
 * Returns the Data id for a sample SyncAdapter contact's profile row, or 0
 * if the sample SyncAdapter user isn't found.
 * //w  w  w  . j a  v a 2  s  . c o  m
 * @param resolver a content resolver
 * @param userId the sample SyncAdapter user ID to lookup
 * @return the profile Data row id, or 0 if not found
 */
private static long lookupProfile(ContentResolver resolver, long userId) {
    long profileId = 0;
    final Cursor c = resolver.query(Data.CONTENT_URI, ProfileQuery.PROJECTION, ProfileQuery.SELECTION,
            new String[] { String.valueOf(userId) }, null);
    try {
        if (c != null && c.moveToFirst()) {
            profileId = c.getLong(ProfileQuery.COLUMN_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return profileId;
}

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

public static ArrayList<String> getFilesFiltered(String filter) {
    ArrayList<String> result = new ArrayList<String>();
    Cursor c = sDB.query(TABLE_DOWNLOADS, new String[] { "path" }, filter, null, null, null, null);
    int numRows = c.getCount();
    Log.i(Constants.LOG_TAG, filter + " : " + numRows);
    for (int i = 0; i < numRows; i++) {
        c.moveToPosition(i);//from   w  ww  .  j ava2  s.c o m
        result.add(c.getString(DL_COL_PATH));
        Log.i(Constants.LOG_TAG, i + " : " + c.getString(DL_COL_PATH));
    }
    c.close();
    return result;
}

From source file:Main.java

/**
 * Consolidates the file path determination functionality of the various
 * media prompts. Beginning with KitKat, the responses use a different
 * mechanism and needs a lot of special handling.
 *
 * @param ctxt/*from w  ww . j a va  2  s  .c om*/
 * @param uri
 * @param pathKey
 * @return
 */
@SuppressLint("NewApi")
public static String getPathFromUri(Context ctxt, Uri uri, String pathKey) {

    if (Build.VERSION.SDK_INT >= 19) {
        return getPath(ctxt, uri);
    } else {
        if (uri.toString().startsWith("file")) {
            return uri.toString().substring(7);
        } else {
            String[] projection = { pathKey };
            Cursor c = null;
            try {
                c = ctxt.getContentResolver().query(uri, projection, null, null, null);
                int column_index = c.getColumnIndexOrThrow(pathKey);
                String path = null;
                if (c.getCount() > 0) {
                    c.moveToFirst();
                    path = c.getString(column_index);
                }
                return path;
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

From source file:com.deliciousdroid.platform.ContactManager.java

private static String lookupUsername(ContentResolver resolver, long id) {
    String result = null;/* w  w w. j a va2s  .co m*/
    final Cursor c = resolver.query(RawContacts.CONTENT_URI, UsernameQuery.PROJECTION, UsernameQuery.SELECTION,
            new String[] { Long.toString(id) }, null);
    try {
        while (c.moveToNext()) {
            result = c.getString(UsernameQuery.COLUMN_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return result;
}

From source file:Main.java

public static String getFirstCNLetterByContactId8(Context context, long contactId) {
    String result = "";
    String[] projection = { "_id", "display_name", "data1", "sort_key" };
    String where = Data.CONTACT_ID + "=?";
    final String sortOrder = null;
    Cursor cursor = context.getContentResolver().query(PHONE_CONTACTS_URI, projection, where,
            new String[] { String.valueOf(contactId) }, sortOrder);

    if (cursor != null) {
        try {//from   ww w  . ja va 2  s.co  m
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String nameLetters = cursor.getString(cursor.getColumnIndexOrThrow("sort_key"));
                    if (null != nameLetters && !"".equals(nameLetters)) {
                        result = nameLetters.substring(0, 1).toUpperCase();
                        break;
                    }
                }
            }

        } finally {
            cursor.close();
        }
    }

    return result;
}

From source file:Main.java

public static LinkedList<Pair<Integer, String>> retrieveIntegerStringPairFromCursor(Cursor cursor,
        String integerColumnName, String stringColumnName) {
    LinkedList<Pair<Integer, String>> result = new LinkedList<Pair<Integer, String>>();

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

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            Integer integerVal = cursor.getInt(cursor.getColumnIndex(integerColumnName));
            String stringVal = cursor.getString(cursor.getColumnIndexOrThrow(stringColumnName));
            result.add(new Pair(integerVal, stringVal));
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return result;
}