String selection = "_id = "+id; Uri uri = Uri.parse("content://sms"); Cursor cursor = contentResolver.query(uri, null, selection, null, null); String phone = cursor.getString(cursor.getColumnIndex("address")); int type = cursor.getInt(cursor.getColumnIndex("type"));// 2 = sent, etc. String date = cursor.getString(cursor.getColumnIndex("date")); String body = cursor.getString(cursor.getColumnIndex("body"));
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while(people.moveToNext()) { int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME); String contact = people.getString(nameFieldColumnIndex); int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER); String number = people.getString(numberFieldColumnIndex); } people.close();
@Override public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode) { case (PICK_CONTACT) : if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); Cursor c = getContentResolver().query(contactData, null, null, null, null); if (c.moveToFirst()) { String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); // TODO Whatever you want to do with the selected contact name. } } break; } }
@Test public void testExecSQL() throws Exception { database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');"); Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM table_name", null); assertThat(cursor).isNotNull(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(1); cursor = database.rawQuery("SELECT * FROM table_name", null); assertThat(cursor).isNotNull(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(cursor.getColumnIndex("id"))).isEqualTo(1234); assertThat(cursor.getString(cursor.getColumnIndex("name"))).isEqualTo("Chuck"); }
public static Uri getAlbumUri(Context context, long musicId) { ContentResolver cr = context.getContentResolver(); Cursor cursor = cr.query(Media.EXTERNAL_CONTENT_URI, proj_music, "_id =" + String.valueOf(musicId), null, null); long id = -3; if (cursor == null) { return null; } if (cursor.moveToFirst()) { id = cursor.getInt(cursor.getColumnIndex(Media.ALBUM_ID)); } cursor.close(); return getAlbumArtUri(id); }
public boolean isRead(String table, String key, int value) { boolean isRead = false; Cursor cursor = mSQLiteDatabase.query(table, null, "key=?", new String[]{key}, null, null, null); if (cursor.moveToNext() && (cursor.getInt(cursor.getColumnIndex("is_read")) == value)) { isRead = true; } cursor.close(); return isRead; }
@Test public void testIsNullWhenNull() throws Exception { assertThat(cursor.moveToFirst()).isTrue(); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.isNull(cursor.getColumnIndex("id"))).isFalse(); assertThat(cursor.isNull(cursor.getColumnIndex("name"))).isFalse(); assertThat(cursor.isNull(cursor.getColumnIndex("long_value"))).isTrue(); assertThat(cursor.isNull(cursor.getColumnIndex("float_value"))).isTrue(); assertThat(cursor.isNull(cursor.getColumnIndex("double_value"))).isTrue(); }
public static final SortedCursor makeRecentTracksCursor(final Context context) { Cursor songs = RecentStore.getInstance(context).queryRecentIds(null); try { return makeSortedCursor(context, songs, songs.getColumnIndex(SongPlayCount.SongPlayCountColumns.ID)); } finally { if (songs != null) { songs.close(); songs = null; } } }
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); while (phones.moveToNext()) { String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close();
@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); }
@Test public void query_shouldReturnColumnValues() throws Exception { ShadowDownloadManager manager = new ShadowDownloadManager(); long id = manager.enqueue(request.setDestinationUri(destination)); Cursor cursor = manager.query(new DownloadManager.Query().setFilterById(id)); cursor.moveToNext(); assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))).isEqualTo(uri.toString()); assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).isEqualTo(destination.toString()); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CONTACT && resultCode == RESULT_OK) { Uri uri = Preconditions.checkNotNull(data.getData()); final Cursor cursor = getContentResolver().query(uri, null, null, null, null); try { if (cursor != null && cursor.moveToFirst()) { final long contactId = cursor.getLong(cursor.getColumnIndex(Contacts._ID)); showContact(contactId); } } finally { if (cursor != null) { cursor.close(); } } return; } super.onActivityResult(requestCode, resultCode, data); }
public static Item valueOf(Cursor cursor) { return new Item(cursor.getLong(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)), cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.MIME_TYPE)), cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns.SIZE)), cursor.getLong(cursor.getColumnIndex("duration"))); }
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case REQ_CODE_PICK_IMAGE: if(resultCode == RESULT_OK){ Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query( selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String filePath = cursor.getString(columnIndex); cursor.close(); Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); } } }
@Test public void testExecSQLInsertNull() throws Exception { String name = "nullone"; database.execSQL("insert into exectable (first_column, name) values (?,?);", new String[]{null, name}); Cursor cursor = database.rawQuery("select * from exectable WHERE `name` = ?", new String[]{name}); cursor.moveToFirst(); int firstIndex = cursor.getColumnIndex("first_column"); int nameIndex = cursor.getColumnIndex("name"); assertThat(cursor.getString(nameIndex)).isEqualTo(name); assertThat(cursor.getString(firstIndex)).isEqualTo(null); }
@Test public void testReplace() throws Exception { long id = addChuck(); assertThat(id).isNotEqualTo(-1L); ContentValues values = new ContentValues(); values.put("id", id); values.put("name", "Norris"); long replaceId = database.replace("table_name", null, values); assertThat(replaceId).isEqualTo(id); String query = "SELECT name FROM table_name where id = " + id; Cursor cursor = executeQuery(query); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getString(cursor.getColumnIndex("name"))).isEqualTo("Norris"); }
DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); long id = mgr.enqueue(request); DownloadManager.Query q = new DownloadManager.Query(); q.setFilterById(id); Cursor cursor = mgr.query(q); cursor.moveToFirst(); int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); cursor.close();
public static SerializableCookie parseCursorToBean(Cursor cursor) { String host = cursor.getString(cursor.getColumnIndex(HOST)); byte[] cookieBytes = cursor.getBlob(cursor.getColumnIndex(COOKIE)); Cookie cookie = bytesToCookie(cookieBytes); return new SerializableCookie(host, cookie); }
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}; Cursor people = getContentResolver().query(uri, projection, null, null, null); int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); if(people.moveToFirst()) { do { String name = people.getString(indexName); String number = people.getString(indexNumber); // Do work... } while (people.moveToNext()); }
public static <T> CacheEntity<T> parseCursorToBean(Cursor cursor) { CacheEntity<T> cacheEntity = new CacheEntity<>(); cacheEntity.setKey(cursor.getString(cursor.getColumnIndex(KEY))); cacheEntity.setLocalExpire(cursor.getLong(cursor.getColumnIndex(LOCAL_EXPIRE))); cacheEntity.setResponseHeaders((HttpHeaders) IOUtils.toObject(cursor.getBlob(cursor.getColumnIndex(HEAD)))); //noinspection unchecked cacheEntity.setData((T) IOUtils.toObject(cursor.getBlob(cursor.getColumnIndex(DATA)))); return cacheEntity; }