Get file path from URI

Solution: public class RealPathUtil { @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 … 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

How to store large blobs in an android content provider?

The solution phreed gives in the bottom half of question is basically correct. I’ll try to add some more details here. When you do getContentResolver().openInputStream(…), content resolver will go to your content provider and call its openFile method. This is how the openFile looks in ContentProvider.java: public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { … Read more

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

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

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

This requires no special permissions, and works with the Storage Access Framework, as well as the unofficial ContentProvider pattern (file path in _data field). /** * 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 … Read more