ContentResolver contentResolver = getContentResolver(); final String[] projection = new String[]{"_id", "ct_t"}; Uri uri = Uri.parse("content://mms-sms/conversations/"); Cursor query = contentResolver.query(uri, projection, null, null, null); if (query.moveToFirst()) { do { String string = query.getString(query.getColumnIndex("ct_t")); if ("application/vnd.wap.multipart.related".equals(string)) { // it's MMS } else { // it's SMS } } while (query.moveToNext()); }
@Override protected int getExifOrientation(Uri uri) { Cursor cursor = null; try { ContentResolver contentResolver = context.getContentResolver(); cursor = contentResolver.query(uri, CONTENT_ORIENTATION, null, null, null); if (cursor == null || !cursor.moveToFirst()) { return 0; } return cursor.getInt(0); } catch (RuntimeException ignored) { // If the orientation column doesn't exist, assume no rotation. return 0; } finally { if (cursor != null) { cursor.close(); } } }
@Override protected int getImageOrientation() throws IOException { Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null); if (cursor == null || cursor.getCount() != 1) { return 0; } cursor.moveToFirst(); int orientation = cursor.getInt(0); cursor.close(); return orientation; } }
@Override public void onClick(View v) { Cursor cursor = getContentResolver().query(URI, null, null, null, null); assert cursor != null; while (cursor.moveToNext()) { int count = cursor.getColumnCount(); StringBuilder sb = new StringBuilder("column: "); for (int i = 0; i < count; i++) { sb.append(cursor.getString(i) + ", "); } Log.d(TAG, sb.toString()); } } });
public static Song getSongFromPath(String songPath, Context context) { ContentResolver cr = context.getContentResolver(); Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String selection = MediaStore.Audio.Media.DATA; String[] selectionArgs = {songPath}; String[] projection = new String[]{"_id", "title", "artist", "album", "duration", "track", "artist_id", "album_id"}; String sortOrder = MediaStore.Audio.Media.TITLE + " ASC"; Cursor cursor = cr.query(uri, projection, selection + "=?", selectionArgs, sortOrder); if (cursor != null && cursor.getCount() > 0) { Song song = getSongForCursor(cursor); cursor.close(); return song; } else return new Song(); }
public String getContactDisplayNameByNumber(String number) { Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); String name = "?"; ContentResolver contentResolver = getContentResolver(); Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null); try { if (contactLookup != null && contactLookup.getCount() > 0) { contactLookup.moveToNext(); name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); //String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID)); } } finally { if (contactLookup != null) { contactLookup.close(); } } return name; }
private int getmCardId() { final ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(Uri.parse("content://media/external/fs_id"), null, null, null, null); int mCardId = -1; if (cursor != null && cursor.moveToFirst()) { mCardId = cursor.getInt(0); cursor.close(); cursor = null; } return mCardId; }
public static final int getSongCountForPlaylist(final Context context, final long playlistId) { Cursor c = context.getContentResolver().query( MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId), new String[]{BaseColumns._ID}, MUSIC_ONLY_SELECTION, null, null); if (c != null) { int count = 0; if (c.moveToFirst()) { count = c.getCount(); } c.close(); c = null; return count; } return 0; }
@NonNull public static String getFileNameFromUri(@NonNull Context context, @NonNull Uri uri) throws FileNotFoundException { try (Cursor cursor = context.getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null)) { if (cursor != null && cursor.moveToFirst()) { return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); } } throw new FileNotFoundException("Could not resolve filename of " + uri); }
private int getmCardId() { final ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(Uri.parse("content://media/external/fs_id"), null, null, null, null); int mCardId = -1; if (cursor != null && cursor.moveToFirst()) { mCardId = cursor.getInt(0); cursor.close(); } return mCardId; }
ContentResolver cr = getContentResolver(); Cursor cur = cr.query(Contacts.CONTENT_URI, null, null, null, null); if(cur != null && cur.moveToFirst()) { String id = cur.getString(cur.getColumnIndex(Contacts._ID)); if (cur.getCount() > 0) { ...
private Cursor openCursorAndGoToFirst(Uri uri, String[] projection, String selection, String[] selectionArgs) { Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, null); if (c == null) { return null; } if (!c.moveToFirst()) { c.close(); return null; } return c; }
ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString( cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null); while (pCur.moveToNext()) { String phoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show(); } pCur.close(); } } }
private Cursor openCursorAndGoToFirst(Uri uri, String[] projection, String selection, String[] selectionArgs) { Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, null); if (c == null) { return null; } if (!c.moveToFirst()) { c.close(); return null; } return c; }
ContentResolver resolver = getContentResolver(); Cursor c = resolver.query( Data.CONTENT_URI, null, Data.HAS_PHONE_NUMBER + "!=0 AND (" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?)", new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE}, Data.CONTACT_ID); while (c.moveToNext()) { long id = c.getLong(c.getColumnIndex(Data.CONTACT_ID)); String name = c.getString(c.getColumnIndex(Data.DISPLAY_NAME)); String data1 = c.getString(c.getColumnIndex(Data.DATA1)); System.out.println(id + ", name=" + name + ", data1=" + data1); }
public static ArtistInfo getArtistinfo(Context context, long id) { ContentResolver cr = context.getContentResolver(); Cursor cursor = cr.query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, proj_artist, "_id =" + String.valueOf(id), null, null); if (cursor == null) { return null; } ArtistInfo artistInfo = new ArtistInfo(); while (cursor.moveToNext()) { artistInfo.artist_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST)); artistInfo.number_of_tracks = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Artists.NUMBER_OF_TRACKS)); } cursor.close(); return artistInfo; }
public static AlbumInfo getAlbumInfo(Context context, long albumId) { ContentResolver cr = context.getContentResolver(); Cursor cursor = cr.query(Albums.EXTERNAL_CONTENT_URI, proj_album, "_id =" + String.valueOf(albumId), null, null); if (cursor == null) { return null; } AlbumInfo albumInfo = new AlbumInfo(); while (cursor.moveToNext()) { albumInfo.album_name = cursor.getString(cursor.getColumnIndex(Albums.ALBUM)); albumInfo.album_art = cursor.getString(cursor.getColumnIndex(Albums.ALBUM_ART)); } cursor.close(); return albumInfo; }
private String getValueForDownloadedFile(Context context, Uri uri, String column) { Cursor cursor = null; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, null, null, null); if (cursor != null && cursor.moveToFirst()) { return cursor.getString(0); } } finally { if (cursor != null) { cursor.close(); } } return null; }
public static String getPath(ContentResolver resolver, Uri uri) { if (uri == null) { return null; } if (SCHEME_CONTENT.equals(uri.getScheme())) { Cursor cursor = null; try { cursor = resolver.query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null); if (cursor == null || !cursor.moveToFirst()) { return null; } return cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)); } finally { if (cursor != null) { cursor.close(); } } } return uri.getPath(); }
private String getValueForDownloadedFile(Context context, Uri uri, String column) { Cursor cursor = null; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, null, null, null); if (cursor != null && cursor.moveToFirst()) { return cursor.getString(0); } } finally { if (cursor != null) { cursor.close(); } } return null; }