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:com.nineash.hutsync.client.NetworkUtilities.java

private static long getCalendar(Account account) {
    // Find the Last.fm calendar if we've got one
    Uri calenderUri = Calendars.CONTENT_URI.buildUpon()
            .appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
            .appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type).build();
    Cursor c1 = mContentResolver.query(calenderUri, new String[] { BaseColumns._ID }, null, null, null);
    if (c1.moveToNext()) {
        long ret = c1.getLong(0);
        c1.close();
        return ret;
    } else {//from w ww. j av  a2s  .  c om
        ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

        ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(Calendars.CONTENT_URI
                .buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
                .appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type).build());
        builder.withValue(Calendars.ACCOUNT_NAME, account.name);
        builder.withValue(Calendars.ACCOUNT_TYPE, account.type);
        builder.withValue(Calendars.NAME, "Pizza Hut Shifts");
        builder.withValue(Calendars.CALENDAR_DISPLAY_NAME, "Pizza Hut Shifts");
        builder.withValue(Calendars.CALENDAR_COLOR, 0xD51007);
        builder.withValue(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_READ);
        builder.withValue(Calendars.OWNER_ACCOUNT, account.name);
        builder.withValue(Calendars.SYNC_EVENTS, 1);
        operationList.add(builder.build());
        try {
            mContentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return -1;
        }
        c1.close();
        return getCalendar(account);
    }
}

From source file:com.csipsimple.utils.SipProfileJson.java

public static JSONObject serializeSipProfile(SipProfile profile, DBAdapter db) {
    JSONObject jsonProfile = serializeBaseSipProfile(profile);
    JSONArray jsonFilters = new JSONArray();

    Cursor c = db.getFiltersForAccount(profile.id);
    int numRows = c.getCount();
    c.moveToFirst();/*from w  w  w . j a v a 2s. c  om*/
    for (int i = 0; i < numRows; ++i) {
        Filter f = new Filter();
        f.createFromDb(c);

        try {
            jsonFilters.put(i, serializeBaseFilter(f));
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Impossible to add fitler", e);
            e.printStackTrace();
        }
        c.moveToNext();
    }
    c.close();

    try {
        jsonProfile.put(FILTER_KEY, jsonFilters);
    } catch (JSONException e) {
        Log.e(THIS_FILE, "Impossible to add fitlers", e);
    }

    return jsonProfile;
}

From source file:Main.java

/**
 * Return text content of clipboard as individual lines 
 * @param ctx//from w w  w  . ja v  a 2 s .c  om
 * @return
 */
@SuppressLint("NewApi")
private static ArrayList<String> getTextLines(Context ctx) {

    String EOL = "\\r?\\n|\\r";

    if (checkForText(ctx)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

            // Gets the clipboard as text.
            CharSequence cs = item.getText();
            if (cs == null) { // item might be an URI
                Uri pasteUri = item.getUri();
                if (pasteUri != null) { // FIXME untested
                    try {
                        Log.d("ClipboardUtils", "Clipboard contains an uri");
                        ContentResolver cr = ctx.getContentResolver();
                        String uriMimeType = cr.getType(pasteUri);
                        //               pasteData = resolveUri(pasteUri);
                        // If the return value is not null, the Uri is a content Uri
                        if (uriMimeType != null) {

                            // Does the content provider offer a MIME type that the current application can use?
                            if (uriMimeType.equals(ClipDescription.MIMETYPE_TEXT_PLAIN)) {

                                // Get the data from the content provider.
                                Cursor pasteCursor = cr.query(pasteUri, null, null, null, null);

                                // If the Cursor contains data, move to the first record
                                if (pasteCursor != null) {
                                    if (pasteCursor.moveToFirst()) {
                                        String pasteData = pasteCursor.getString(0);
                                        return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
                                    }
                                    // close the Cursor
                                    pasteCursor.close();
                                }
                            }
                        }
                    } catch (Exception e) { // FIXME given that the above is unteted, cath all here
                        Log.e("ClipboardUtils", "Resolving URI failed " + e);
                        e.printStackTrace();
                        return null;
                    }
                }
            } else {
                Log.d("ClipboardUtils", "Clipboard contains text");
                String pasteData = cs.toString();
                return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
            }
        } else {
            // Gets the clipboard as text.
            @SuppressWarnings("deprecation")
            CharSequence cs = oldClipboard.getText();
            if (cs != null) {
                String pasteData = cs.toString();
                if (pasteData != null) { // should always be the case
                    return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
                }
            }
        }
        Log.e("ClipboardUtils", "Clipboard contains an invalid data type");
    }
    return null;
}

From source file:com.android.emailcommon.utility.AttachmentUtilities.java

/**
 * Resolve attachment id to content URI.  Returns the resolved content URI (from the attachment
 * DB) or, if not found, simply returns the incoming value.
 *
 * @param attachmentUri//from  w  w  w.ja  v  a  2 s . c  o  m
 * @return resolved content URI
 *
 * TODO:  Throws an SQLite exception on a missing DB file (e.g. unknown URI) instead of just
 * returning the incoming uri, as it should.
 */
public static Uri resolveAttachmentIdToContentUri(ContentResolver resolver, Uri attachmentUri) {
    Cursor c = resolver.query(attachmentUri, new String[] { Columns.DATA }, null, null, null);
    if (c != null) {
        try {
            if (c.moveToFirst()) {
                final String strUri = c.getString(0);
                if (strUri != null) {
                    return Uri.parse(strUri);
                }
            }
        } finally {
            c.close();
        }
    }
    return attachmentUri;
}

From source file:Main.java

public static boolean haveCalendars(Activity ctx) {

    boolean exists = false;

    String[] projection = new String[] { "_id", "name" };
    Uri calendars = Uri.parse("content://calendar/calendars");

    // Calendar uri changes for 2.2
    Uri calendars_2_2 = Uri.parse("content://com.android.calendar/calendars");
    Cursor managedCursor = ctx.managedQuery(calendars, projection, null, null, null);

    if (managedCursor == null || managedCursor.getCount() == 0)
        managedCursor = ctx.managedQuery(calendars_2_2, projection, null, null, null);

    if (managedCursor != null && managedCursor.moveToFirst()) {
        String calName;/*from   w  w w .  j a  va  2  s . c o  m*/
        String calId;
        int nameColumn = managedCursor.getColumnIndex("name");
        int idColumn = managedCursor.getColumnIndex("_id");
        do {
            calName = managedCursor.getString(nameColumn);
            calId = managedCursor.getString(idColumn);
            exists = true;
        } while (managedCursor.moveToNext());

        managedCursor.close();
    }

    return exists;
}

From source file:Main.java

public static File convertImageUriToFile(Uri imageUri, Activity activity) {
    Cursor cursor = null;
    try {/*  w w w .  j a  v a 2 s.c o  m*/
        String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,
                MediaStore.Images.ImageColumns.ORIENTATION };
        cursor = activity.managedQuery(imageUri, proj, null, null, null);
        int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
        if (cursor.moveToFirst()) {
            String orientation = cursor.getString(orientation_ColumnIndex);
            return new File(cursor.getString(file_ColumnIndex));
        }
        return null;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

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 {//w w w. j  a v  a  2 s.c  om
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            String value = cursor.getString(column_index);
            if (value.startsWith("content://") || !value.startsWith("/") && !value.startsWith("file://")) {
                return null;
            }
            return value;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:com.csipsimple.backup.SipProfileJson.java

public static JSONObject serializeSipProfile(Context context, SipProfile profile) {
    JSONObject jsonProfile = serializeBaseSipProfile(profile);
    JSONArray jsonFilters = new JSONArray();

    Cursor c = Filter.getFiltersCursorForAccount(context, profile.id);
    int numRows = c.getCount();
    c.moveToFirst();/*w  w w  . j a va2  s . c  o m*/
    for (int i = 0; i < numRows; ++i) {
        Filter f = new Filter(c);
        try {
            jsonFilters.put(i, serializeBaseFilter(f));
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Impossible to add fitler", e);
        }
        c.moveToNext();
    }
    c.close();

    try {
        jsonProfile.put(FILTER_KEY, jsonFilters);
    } catch (JSONException e) {
        Log.e(THIS_FILE, "Impossible to add fitlers", e);
    }

    return jsonProfile;
}

From source file:com.cyberocw.habittodosecretary.file.StorageHelper.java

public static String getRealPathFromURI(Context mContext, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = mContext.getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor == null) {
        return null;
    }//from   w  ww  .  j  a  v a 2  s. c  o  m
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String path = cursor.getString(column_index);
    cursor.close();
    return path;
}

From source file:eu.e43.impeller.Utils.java

public static JSONObject findPost(Context ctx, Content.Uris uris, JSONObject object) throws JSONException {
    Cursor res = ctx.getContentResolver().query(uris.activitiesUri, new String[] { "_json" },
            "actor=? AND verb='post' AND object.id=?",
            new String[] { object.getJSONObject("author").getString("id"), object.getString("id") }, null);

    try {/*from   w w w .j  ava 2 s .  com*/
        if (res.getCount() > 0) {
            res.moveToFirst();
            return new JSONObject(res.getString(0));
        } else {
            return null;
        }
    } finally {
        res.close();
    }
}