Libathos
Libathos

Reputation: 3362

Invalid URI when trying to convert it into bitmap

what I want to do is have the user choose a contact and then set the corresponding imageView from my application as the contact's photo (if any). With what I have written so far I can get the URI of the contact's photo but then I get this exception read failed: EINVAL (invalid argument) at first I thought it could be because of a null value though the URI I get is content://com.android.contacts/contacts/1/photo is this an invalid URI? the way I try to set it in the imageView is as follows:

            if (myUri != null){
            try {
                bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(myUri));
                firstImage.setImageBitmap(bitmap);              
            } catch (FileNotFoundException e) { 
                ;
            } 

How can this be fixed?

Upvotes: 0

Views: 179

Answers (1)

electrofant
electrofant

Reputation: 945

With the READ_CONTACTS permission you are able to do this:

Uri myUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactId);

if (myUri != null){
    try {
        InputStream in = ContactsContract.Contacts
                .openContactPhotoInputStream(getActivity().getContentResolver(), myUri);
        bitmap = BitmapFactory.decodeStream(in);
        firstImage.setImageBitmap(bitmap);              
    } catch (FileNotFoundException e) { 
            ;
} 

Upvotes: 1

Related Questions