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

@SuppressLint("NewApi")
public static String getRealPathFromUriApi19(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 selection = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            selection, new String[] { id }, null);

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

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }//www  . j av a 2 s . c o m
    cursor.close();
    return filePath;
}

From source file:Main.java

/**
 * Gets the Uri to a specific audio file
 * @param filePath The path of the file that we are looking up
 * @param contentResolver The content resolver that is used to perform the query
 * @return The Uri of the sound file//  ww w  . ja  v  a 2  s . c  om
 */
private static Uri getAudioUriFromFilePath(String filePath, ContentResolver contentResolver) {
    long audioId;
    Uri uri = MediaStore.Audio.Media.getContentUri("external");
    String[] projection = { BaseColumns._ID };
    Cursor cursor = contentResolver.query(uri, projection, MediaStore.MediaColumns.DATA + " LIKE ?",
            new String[] { filePath }, null);

    if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(projection[0]);
        audioId = cursor.getLong(columnIndex);
        cursor.close();
        return Uri.parse(uri.toString() + "/" + audioId);
    }
    return null;
}

From source file:Main.java

/**
 * Get a uri's file path//from  w  ww  .ja va  2  s .  c  o m
 * 
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return the file path
 */
public static String getUriPath(Context context, Uri uri) {
    String filePath = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] projection = { MediaStore.Files.FileColumns.DATA };

        /* 
         * FIXME 2013-10-24 Tianzi Hou
         * 
         * we cannot get file path if it is from 
         *  content://com.google.android.gallery3d.provider
         * i.e. the Picasa service
         */
        filePath = null;
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(projection[0]);
            cursor.moveToFirst();
            filePath = cursor.getString(column_index);
            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        filePath = uri.getPath();
    }

    return filePath;
}

From source file:Main.java

/**
 * @param mContext//from   www .ja  v  a2  s.co m
 * @param number
 * @return sunrise.l String 2012-5-28
 */
public static String getNameFormNumber(Context mContext, String number) {
    String name = null;

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number);
    Cursor cur = mContext.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
    if (cur != null && cur.moveToFirst()) {
        int nameIndex = cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
        name = cur.getString(nameIndex);
    }
    cur.close();
    return name;
}

From source file:Main.java

/**
 * Get video's duration from {@link ContentProvider}
 *
 * @param context//  w  w  w  .  jav a  2s .c o  m
 * @param uri     must has {@link Uri#getScheme()} equals
 *                {@link ContentResolver#SCHEME_CONTENT}
 * @return Duration of video, in milliseconds.
 */
public static long getDuration(Context context, Uri uri) {
    long duration = 0L;
    Cursor cursor = MediaStore.Video.query(context.getContentResolver(), uri,
            new String[] { MediaStore.Video.VideoColumns.DURATION });
    if (cursor != null) {
        cursor.moveToFirst();
        duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DURATION));
        cursor.close();
    }
    return duration;
}

From source file:Main.java

public static int getOrientation(Context context, Uri photoUri) {
    int orientation = 0;
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
    if (cursor != null) {
        if (cursor.getCount() != 1) {
            return -1;
        }/* ww  w. j  a v  a  2  s .  c  o m*/
        cursor.moveToFirst();
        orientation = cursor.getInt(0);
        cursor.close();
    }
    return orientation;
}

From source file:Main.java

public static String getNewThreadID(Context context) {
    int new_id = 0;
    final String[] projection = new String[] { "_id" };
    Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
    Cursor query = context.getContentResolver().query(uri, projection, null, null, "date DESC");
    if (query != null) {
        while (query.moveToNext()) {
            int tmp = query.getInt(query.getColumnIndexOrThrow("_id")) + 1;
            if (tmp > new_id) {
                new_id = tmp;
            }/*from  w w  w. jav a  2s  . com*/
        }
        query.close();
    }
    return "" + new_id;
}

From source file:Main.java

public static Uri getBitmapFromImageId(Activity activity, int imageId) {
    String[] tinyImgPprojection = { MediaStore.Images.Thumbnails._ID };
    Cursor tinyCursor = Thumbnails.queryMiniThumbnail(activity.getContentResolver(), imageId,
            Thumbnails.MINI_KIND, tinyImgPprojection);
    if (tinyCursor.getCount() != 0) {
        tinyCursor.moveToFirst();//from  w  w w .  j  a  v  a2 s .co  m
        int tinyImgId = tinyCursor.getInt(tinyCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
        tinyCursor.close();
        return Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                String.valueOf(tinyImgId));
    } else {
        tinyCursor.close();
        return null;
    }
}

From source file:edu.mit.mobile.android.locast.data.tags.TaggableUtils.java

public static Set<String> getTags(ContentProviderClient cpc, Uri tagDir) throws RemoteException {
    final Set<String> tags = new HashSet<String>();

    final Cursor c = cpc.query(tagDir, TAG_PROJECTION, null, null, null);

    try {//from  ww w  .  jav  a 2s . c o m
        final int tagNameCol = c.getColumnIndexOrThrow(Tag.COL_NAME);

        while (c.moveToNext()) {
            tags.add(c.getString(tagNameCol));
        }
    } finally {
        c.close();
    }

    return tags;
}

From source file:Main.java

static String getAccessTokenFromTable(Context context, String tableName) {
    String token = null;//w  w  w. j av  a2 s .c  o  m
    try {
        SQLiteDatabase db = context.openOrCreateDatabase(DEPRECATED_DATABASE_NAME, 0, null);
        Cursor c = db.rawQuery(
                "SELECT " + DEPRECATED_ACCESS_TOKEN_COLUMN + " FROM " + tableName + " WHERE local_id=0", null);
        if (c.moveToFirst() && c.getColumnIndex(DEPRECATED_ACCESS_TOKEN_COLUMN) != -1) {
            token = c.getString(c.getColumnIndex(DEPRECATED_ACCESS_TOKEN_COLUMN));
        }
        c.close();
        db.close();
    } catch (SQLException e) {
        // DB doesn't exist
    }
    return token;
}