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 int getLastImageId(Context context) {
    final String[] imageColumns = { Images.Media._ID };
    final String imageOrderBy = Images.Media._ID + " DESC";
    Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null,
            null, imageOrderBy);/*from   ww  w.  jav  a 2 s .c om*/
    if (cursor == null)
        return 0;
    int id = 0;
    if (cursor.moveToFirst()) {
        id = cursor.getInt(cursor.getColumnIndex(Images.Media._ID));
    }
    cursor.close();
    return id;
}

From source file:Main.java

/**
 * Returns the system default ringtone URI {@link Uri}, or
 * the first available ringtone when the system default
 * does not exist (usually tablets)./*  w  w w.  j a  v a  2  s . c  o  m*/
 *
 * @param ctx the {@link Context}
 * @return Uri ringtone URI
 */
public static Uri getDefaultRingtoneUri(Context ctx, int type) {
    Uri uri;

    uri = RingtoneManager.getActualDefaultRingtoneUri(ctx, type);

    if (uri == null) {
        // The default ringtone doesn't exist - probably a tablet
        // Return the first available
        RingtoneManager rm = new RingtoneManager(ctx);
        rm.setType(type);

        Cursor cursor = rm.getCursor();
        cursor.moveToFirst();

        String idString = cursor.getString(RingtoneManager.ID_COLUMN_INDEX);
        String uriString = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);

        uri = Uri.parse(uriString + '/' + idString);

        cursor.close();

        return uri;
    } else {
        // Return system default ringtone
        return uri;
    }
}

From source file:Main.java

/**
 * Handles pre V19 uri's/*from   w ww  .  jav  a2s . co  m*/
 * @param context
 * @param contentUri
 * @return
 */
private static String getPathForPreV19(Context context, Uri contentUri) {
    String res = null;

    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            res = cursor.getString(column_index);
        }
        cursor.close();
    }

    return res;
}

From source file:Main.java

public static boolean isExistShortcut(Activity context, String authorities) {
    boolean isInstallShortcut = false;
    final ContentResolver cr = context.getContentResolver();

    /*//from ww w  .  j  a  v  a 2 s .c  o m
     * if (android.os.Build.VERSION.SDK_INT < 8) { AUTHORITIES =
     * "com.android.launcher.settings"; } else { AUTHORITIES =
     * "com.android.launcher2.settings"; }
     */
    final Uri CONTENT_URI = Uri.parse("content://" + authorities + "/favorites?notify=true");
    Cursor c = cr.query(CONTENT_URI, new String[] { "iconPackage" }, "iconPackage=?",
            new String[] { context.getApplication().getPackageName() }, null);
    if (c != null) {
        if (c.getCount() > 0) {
            isInstallShortcut = true;
        }
        c.close();
    }
    return isInstallShortcut;
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
    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);
    }//from w  w  w . java 2  s.  co  m
    cursor.close();
    return filePath;
}

From source file:Main.java

public static long[] getSongListForArtist(Context context, long id) {
    final String[] ccols = new String[] { MediaStore.Audio.Media._ID };
    String where = MediaStore.Audio.Media.ARTIST_ID + "=" + id + " AND " + MediaStore.Audio.Media.IS_MUSIC
            + "=1";
    Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, ccols, where, null,
            MediaStore.Audio.Media.ALBUM_KEY + "," + MediaStore.Audio.Media.TRACK);

    if (cursor != null) {
        long[] list = getSongListForCursor(cursor);
        cursor.close();
        return list;
    }/*from ww  w  . j av  a  2 s  . c  om*/
    return sEmptyList;
}

From source file:Main.java

public static int getOrientation(Context context, Uri contentUri) {
    Cursor cursor = context.getContentResolver().query(contentUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    try {/*from  ww  w.ja  v a2  s . com*/
        if (cursor.moveToFirst()) {
            return cursor.getInt(0);
        } else {
            return -1;
        }
    } finally {
        cursor.close();
    }
}

From source file:Main.java

public static long[] getSongListForPlaylist(Context context, long plid) {
    final String[] ccols = new String[] { MediaStore.Audio.Playlists.Members.AUDIO_ID };
    Cursor cursor = query(context, MediaStore.Audio.Playlists.Members.getContentUri("external", plid), ccols,
            null, null, MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);

    if (cursor != null) {
        long[] list = getSongListForCursor(cursor);
        cursor.close();
        return list;
    }//from  w  w  w. j a v  a  2  s.  co  m
    return sEmptyList;
}

From source file:Main.java

public static Long getOrCreateThreadId(Context context, String phone) {
    try {//from   ww w .  jav a 2  s  .c  o m
        Uri threadIdUri = Uri.parse("content://mms-sms/threadID");
        Uri.Builder builder = threadIdUri.buildUpon();
        String[] recipients = { phone };
        for (String recipient : recipients) {
            builder.appendQueryParameter("recipient", recipient);
        }
        Uri uri = builder.build();
        Long threadId = 0L;
        Cursor cursor = context.getContentResolver().query(uri, new String[] { "_id" }, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    threadId = cursor.getLong(0);
                }
            } finally {
                cursor.close();
            }
            return threadId;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1L;
}

From source file:Main.java

/**
 * Get a uri's user-friendly display name
 * //from w w w . j  a  va 2  s .c  om
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return a user-friendly display name
 */
public static String getUriDisplayName(Context context, Uri uri) {
    String displayName = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] proj = { OpenableColumns.DISPLAY_NAME };
        Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);

        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
            cursor.moveToFirst();
            displayName = cursor.getString(columnIndex);

            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        displayName = uri.getLastPathSegment();
    }

    return displayName;
}