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 file path from a Media storage URI.
 *///from  ww  w  .j a  v a 2  s  .c  o m
public static String getPathFromURI(ContentResolver contentResolver, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = contentResolver.query(contentUri, proj, null, null, null);
    if (cursor == null) {
        return null;
    }
    try {
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (!cursor.moveToFirst()) {
            return null;
        } else {
            return cursor.getString(columnIndex);
        }
    } finally {
        cursor.close();
    }
}

From source file:com.goliathonline.android.kegbot.io.RemoteUserHandler.java

private static ContentValues queryUserDetails(Uri uri, ContentResolver resolver) {
    final ContentValues values = new ContentValues();
    final Cursor cursor = resolver.query(uri, UserQuery.PROJECTION, null, null, null);
    try {//w  w w  .  j  a v  a2s  .  co  m
        if (cursor.moveToFirst()) {
            values.put(SyncColumns.UPDATED, cursor.getLong(UserQuery.UPDATED));
        } else {
            values.put(SyncColumns.UPDATED, KegbotContract.UPDATED_NEVER);
        }
    } finally {
        cursor.close();
    }
    return values;
}

From source file:Main.java

/**
 * Using code from this link: http://hmkcode.com/android-display-selected-image-and-its-real-path/
 * This method will return the absolute path Android.net.Uri.
 * NOTE!!! THIS DOES NOT SUPPORT API 10 OR BELOW!!! IF YOU NEED TO WORK WITH THAT, CHECK LINK ABOVE
 * @param context Context/*from ww  w  . j  av  a 2 s . c  o  m*/
 * @param uri Uri to check
 * @return String for the absolute path
 */
public static String getAbsolutePath(Context context, android.net.Uri uri) {
    if (Build.VERSION.SDK_INT >= 19) {
        try {
            String filePath = "";
            String wholeID = DocumentsContract.getDocumentId(uri);

            // 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);

            int columnIndex = cursor.getColumnIndex(column[0]);

            if (cursor.moveToFirst()) {
                filePath = cursor.getString(columnIndex);
            }
            cursor.close();
            return filePath;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    } else {
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            String result = null;

            CursorLoader cursorLoader = new CursorLoader(context, uri, proj, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();

            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                result = cursor.getString(column_index);
            }
            return result;

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

From source file:fr.mixit.android.io.JSONHandler.java

/**
 * Returns those id's from a {@link android.net.Uri} that were not found in a given set.
 *//*from   w  ww  .  j  a  va2 s  .co m*/
protected static HashSet<String> getLostIds(Set<String> ids, Uri uri, String[] projection, int idColumnIndex,
        ContentResolver resolver) {
    final HashSet<String> lostIds = Sets.newHashSet();

    final Cursor cursor = resolver.query(uri, projection, null, null, null);
    try {
        while (cursor.moveToNext()) {
            final String id = cursor.getString(idColumnIndex);
            if (!ids.contains(id)) {
                lostIds.add(id);
            }
        }
    } finally {
        cursor.close();
    }

    if (!lostIds.isEmpty()) {
        Log.d(TAG, "Found " + lostIds.size() + " for " + uri.toString() + " that need to be removed.");
    }

    return lostIds;
}

From source file:Main.java

/**
 * Reads the data from the {@code column} of the content's {@code queryUri} and returns it as an
 * Array.//  w  w w .  java 2s  .c o  m
 */
static private Set<String> getColumnContentAsArray(Context context, Uri queryUri, String column) {
    Cursor cursor = context.getContentResolver().query(queryUri, new String[] { column }, null, null, null);
    Set<String> columnValues = new HashSet<>();
    try {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                columnValues.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return columnValues;
}

From source file:com.example.facebook_photo.Utility.java

public static int getOrientation(Context context, Uri photoUri) {
    /* it's on the external media. */
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    if (cursor.getCount() != 1) {
        return -1;
    }// w w w  .j av a  2s  .co  m

    cursor.moveToFirst();
    int orientation = cursor.getInt(0);
    cursor.close();

    return orientation;
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

private static void close(Cursor cursor, SQLiteDatabase database) {
    if (cursor != null) {
        cursor.close();
    }/*from   w  ww . j a  v  a  2 s.  com*/
    if (database != null) {
        database.close();
    }
}

From source file:Main.java

/**
 * Try to return the absolute file path from the given Uri
 * //  w  ww  . ja  va  2s. c om
 * @param context
 * @param uri
 * @return the file path or null
 */
public static String getRealFilePath(final Context context, final Uri uri) {

    if (null == uri)
        return null;

    final String scheme = uri.getScheme();
    String data = null;

    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
                null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:Main.java

/**
 * Get the real file path from URI registered in media store 
 * @param contentUri URI registered in media store 
 *//*from w w  w .  j ava 2s  .c  o m*/
public static String getRealPathFromURI(Activity activity, Uri contentUri) {

    String releaseNumber = Build.VERSION.RELEASE;

    if (releaseNumber != null) {
        /* ICS Version */
        if (releaseNumber.length() > 0 && releaseNumber.charAt(0) == '4') {
            String[] proj = { MediaStore.Images.Media.DATA };
            String strFileName = "";
            CursorLoader cursorLoader = new CursorLoader(activity, contentUri, proj, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                if (cursor.getCount() > 0)
                    strFileName = cursor.getString(column_index);

                cursor.close();
            }
            return strFileName;
        }
        /* GB Version */
        else if (releaseNumber.startsWith("2.3")) {
            String[] proj = { MediaStore.Images.Media.DATA };
            String strFileName = "";
            Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                cursor.moveToFirst();
                if (cursor.getCount() > 0)
                    strFileName = cursor.getString(column_index);

                cursor.close();
            }
            return strFileName;
        }
    }

    //---------------------
    // Undefined Version
    //---------------------
    /* GB, ICS Common */
    String[] proj = { MediaStore.Images.Media.DATA };
    String strFileName = "";
    Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        // Use the Cursor manager in ICS         
        activity.startManagingCursor(cursor);

        cursor.moveToFirst();
        if (cursor.getCount() > 0)
            strFileName = cursor.getString(column_index);

        //cursor.close(); // If the cursor close use , This application is terminated .(In ICS Version)
        activity.stopManagingCursor(cursor);
    }
    return strFileName;
}

From source file:Main.java

public static File parseFileByIntentData(Context context, Intent data) {
    File file = null;/*from   w w  w  .j av a2 s .c om*/
    if (data != null && data.getData() != null) {
        String[] proj = { MediaStore.Images.Media.DATA };
        CursorLoader cursorLoader = new CursorLoader(context, data.getData(), proj, null, null, null);
        Cursor cursor = null;
        try {
            cursor = cursorLoader.loadInBackground();
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            file = new File(cursor.getString(column_index));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    return file;
}