Insertion of thousands of contact entries using applyBatch is slow

Use ContentResolver.bulkInsert (Uri url, ContentValues[] values) instead of ApplyBatch() ApplyBatch (1) uses transactions and (2) it locks the ContentProvider once for the whole batch instead locking/unlocking once per operation. because of this, it is slightly faster than doing them one at a time (non-batched). However, since each Operation in the Batch can have a different … Read more

How to remove a contact programmatically in android

To delete all contacts use the following code: ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cur.moveToNext()) { try{ String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); System.out.println(“The uri is ” + uri.toString()); cr.delete(uri, null, null); } catch(Exception e) { System.out.println(e.getStackTrace()); } } To delete any specific contact modify … Read more

Create/Copy File in Android Q using MediaStore

NOTE: If you reinstall the app, MediaStore will not recognize the previous-created file any more: Android 11 cannot retrieve files create with MediaStore after the app re-installs, using Intent to let user pick file is the only solution. 1. Create and Write File createAndWriteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { ContentValues … Read more

List all camera images in Android

The Gallery app obtains camera images by using a content resolver over Images.Media.EXTERNAL_CONTENT_URI and filtering the results by Media.BUCKET_ID. The bucket identifier is determined with the following code: public static final String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString() + “/DCIM/Camera”; public static final String CAMERA_IMAGE_BUCKET_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME); /** * Matches code in MediaProvider.computeBucketValues. Should be a common * … Read more

Convert content:// URI to actual path in Android 4.4

This will get the file path from the MediaProvider, DownloadsProvider, and ExternalStorageProvider, while falling back to the unofficial ContentProvider method you mention. /** * Get a file path from a Uri. This will get the the path for Storage Access * Framework Documents, as well as the _data field for the MediaStore and * other … Read more

Android fetch all contact list (name, email, phone) takes more then a minute for about 700 contacts

with the following code for 59 contacts i got the following results on the emulator: D ╔══════ query execution stats ═══════ D ║ got 59 contacts D ║ query took 0.012 s (12 ms) D ╚════════════════════════════════════ ok, that was the best time, but the average is 25-35 ms (for 59 contacts), add the following code … Read more

Android Gallery on Android 4.4 (KitKat) returns different URI for Intent.ACTION_GET_CONTENT

Just wanted to say that this answer is brilliant and I’m using it for a long time without problems. But some time ago I’ve stumbled upon a problem that DownloadsProvider returns URIs in format content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fdoc.pdf and hence app is crashed with NumberFormatException as it’s impossible to parse its uri segments as long. But raw: segment … Read more