Showing posts with label android-contentresolver. Show all posts
Showing posts with label android-contentresolver. Show all posts

Saturday, January 27, 2018

Content resolver returns wrong size

Leave a Comment

I am picking an image from the gallery and querying its size via ContentResolver API, it returns 29kb.

However when I check the file via adb using ls -al it is 44kb

here is how I query the size of the image:

    private fun getFileInfo(contentResolver: ContentResolver, fileUri: Uri): Pair<String, Long>? {     val projection = arrayOf(MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.SIZE)     val metaCursor = contentResolver.query(fileUri, projection, null, null, null)         metaCursor?.use {             if (it.moveToFirst()) {                 val displayNameIndex = it.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)                 val sizeIndex = it.getColumnIndex(MediaStore.MediaColumns.SIZE)                 return Pair(it.getString(displayNameIndex), it.getLong(sizeIndex))             }     }     return null } 

I am using an Oreo Emulator. I have also checked via emulator's tools, file browser shows as 29kb on the other hand file details shows 45kb. What is going on?

Here are the images from file browser:

Summary

Details

Another side note, above situation can be reproducible every time when using camera app on emulator with Android 26-Oreo emulator, however it is fine with emulator Android 25-Nougat.

I have checked, new Document API also returns 29kb

1 Answers

Answers 1

Android 8.0 (Oreo) introduced the 'SDCardFS' filesystem, which replaced 'FUSE' on the /sdcard partition.

it is possible that this new filesystem has a large cluster size and is therefore not able to allocate exactly 29kb, but instead the smallest block larger than 29kb, which in your case is 44.*kb.

The difference between ls -al and file details is probably caused by different rounding.

Read More

Tuesday, March 21, 2017

I want to get whatsApp profilepicture but only getting name and contactnumber?

Leave a Comment

I want to get WhatsApp profile picture and number but using contentResolver I will getting only name and number using following snippet code.

private void showContactWhatsApp(){      ContentResolver cr = getContentResolver();      Cursor contactCursor = cr.query(             ContactsContract.RawContacts.CONTENT_URI,             new String[]{ContactsContract.RawContacts._ID,                     ContactsContract.RawContacts.CONTACT_ID},             ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?",             new String[]{"com.whatsapp"},             null);       ArrayList<String> myWhatsappContacts = new ArrayList<>();      if (contactCursor != null) {         if (contactCursor.getCount() > 0) {             if (contactCursor.moveToFirst()) {                 do {                     //whatsappContactId for get Number,Name,Id ect... from  ContactsContract.CommonDataKinds.Phone                     String whatsappContactId = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));                      if (whatsappContactId != null) {                         //Get Data from ContactsContract.CommonDataKinds.Phone of Specific CONTACT_ID                         Cursor whatsAppContactCursor = cr.query(                                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                                 new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID,                                         ContactsContract.CommonDataKinds.Phone.NUMBER,                                         ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",                                 new String[]{whatsappContactId}, null);                          if (whatsAppContactCursor != null) {                             whatsAppContactCursor.moveToFirst();                             String id = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));                             String name = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));                             String number = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                              whatsAppContactCursor.close();                              //Add Number to ArrayList                             myWhatsappContacts.add(number);                              Log.e(TAG, " WhatsApp contact id  :  " + id);                             Log.e(TAG, " WhatsApp contact name :  " + name);                             Log.e(TAG, " WhatsApp contact number :  " + number);                         }                     }                 } while (contactCursor.moveToNext());                 contactCursor.close();             }         }     }      Log.e(TAG, " WhatsApp contact size :  " + myWhatsappContacts.size()); } 

I want to get WhatsApp profile picture like SyncMe app.

I wait to get WhatsApp contact list with name, number, and thumbnail.

1 Answers

Answers 1

Firstly I just want to reiterate the difference between a RawContact and a Contact. The latter being an aggregation (representing a person) of the former (representing an account).

Depending on what use you have for the image, it may be better to fetch the aggregate Contact and use the Profile image selected there which can be achieved via the Photo contract.

Uri photoUri; Cursor photoCur = cr.query(     ContactsContract.Data.CONTENT_URI, null,     ContactsContract.Data.CONTACT_ID + "=" + aggregateContactId + " AND " +      ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'",      null, null ); if (photoCur != null && photoCur.moveToFirst()) {     Uri photoId = ContentUris.withAppendedId(Contacts.CONTENT_URI, aggregateContactId);     photoUri = Uri.withAppendedPath(person, Contacts.Photo.CONTENT_DIRECTORY); } 

If you have use specifically for the WhatsApp photo, first make sure the photo is cached on the device (just to avoid false flags while testing - open the contacts full image in WhatsApp and you can be sure it's cached) then you'll have to do a separate lookup for the image (from API 14):

Uri photoId = ContentUris.withAppendedId(RawContacts.CONTENT_URI, whatsappContactId); Uri photoUri = Uri.withAppendedPath(photoId, RawContacts.DisplayPhoto.CONTENT_DIRECTORY); 
Read More

Friday, May 6, 2016

Android - Getting Contact List with street addresses but no low value ones like Skype where the address is only a city and state

Leave a Comment

I got a cursor retrieving all the contacts on the app that have a street address. This cursor is then passed into an Adapter. So far so good. Except I also get a bunch of low value contacts (mostly from Skype) that only have a State/Country info. Is there an easy way to modify the URI to skip those?

public Cursor getDirectoryList (CharSequence constraint)  {          String[] selectionArguments = { "%"+constraint.toString()+"%" };         String selection = ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME + " like ?";          Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;         String sortOrder = ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";         Cursor cr = getContentResolver().query(uri, null, selection, selectionArguments, sortOrder);          return cr;     } 

1 Answers

Answers 1

You can modify the selection and/or selectionArgs to specify more criteria, such as ensuring that the Street field is not null:

String selection =     ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME + " like ? AND " +     ContactsContract.CommonDataKinds.StructuredPostal.STREET + " IS NOT NULL"; 

This is just SQL, so specify as many fields from ContactsContract.CommonDataKinds.StructuredPostal, and whatever conditions on them, that you want.

Read More