4

So in an app I am building I fetch all the contacts within the phone and display it in a list view. Everything worked fine up until I upgraded my phone(HTC One)from 4.1.2 to 4.4.2. I no longer get images/profile pics of contacts instead it throws a FileNotFoundException. Also this happens only for a few contacts and not all. I am able to get the pictures from a few contacts and it fails for a few. The same piece of code works absolutely fine on a Nexus 5 running 4.4.2.

Here is the code which I use for querying the Contacts :

 ContentResolver cr = getActivity().getContentResolver();

     String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                     ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.PHOTO_URI, 
                     ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI, ContactsContract.CommonDataKinds.Phone.CONTACT_ID};

     String SELECTION =
                (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY) +
                "<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";

     String sortBy = ContactsContract.Contacts.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";

     Cursor people = cr.query(uri, projection, SELECTION + " AND " + ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER
             + "=1", null, sortBy);

The logcat error which I get is :

System.out(17898): resolveUri failed on bad bitmap uri: content://com.android.contacts/contacts/289/photo
ImageView(17898): Unable to open content: content://com.android.contacts/contacts/289/photo
ImageView(17898): java.io.FileNotFoundException: content://com.android.contacts/contacts/289/photo?restricted=true
ImageView(17898):   at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
ImageView(17898):   at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:737)

Any ideas as to what could be the problem? Any suggestions?

Thanks in advance

Edit : I build a custom object named Contact with all the details and set it to a custom adapter. Here is the code where I set the photo in the imageview, though I don't think this is relevant :

 if(contact != null){
        viewHolder.contactName.setText(contact.getName());
        if(contact.getThumbUri() != null){
            viewHolder.contactThumb.setImageURI(Uri.parse(contact.getThumbUri()));
        }else{
            viewHolder.contactThumb.setImageResource(R.drawable.ic_launcher);
        }
        if(contact.isTlknUser()){
            isTlkn[position] = true;
        }
    }
Adnan Mulla
  • 2,872
  • 3
  • 25
  • 35
  • Does the answer to this question (http://stackoverflow.com/questions/19836455/getting-bitmap-from-contacts-fails-even-though-bitmap-uri-is-not-null) help? Could you post the code used to actually load the picture from the URI? – Paul-Jan Apr 14 '14 at 07:15
  • @Paul-Jan Apparently no. I just use setImageURI method in getView to set the thumbnail. – Adnan Mulla Apr 14 '14 at 07:53

1 Answers1

0

This is a specific build issue I suppose, because once I flashed a different ROM this problem was automatically resolved. Still don't know what the problem was, but yes since it works fine on another ROM, I am assuming its just a particular build of Kitkat or other OEMS which is affected by this.

Adnan Mulla
  • 2,872
  • 3
  • 25
  • 35
  • Could you post what ROM you were having this problem with? Working with a very similar issue on HTC One M7 4.4.3 Android Revolution HD and not sure if it's a device/ROM-specific issue or whether there's a better way for accessing the photos. – jkau Jan 15 '15 at 13:13
  • I was on Insertcoin ROM. Flashing a different ROM solved the problem. Not sure what the problem was – Adnan Mulla Jan 15 '15 at 13:17