Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I verify image URI is valid in Android?

I am building my own contact picker, because I needed multi-select support. Everything is working fine, except for one small problem with the contact images.

For contacts who don't have images I am showing a "no image" image. This works fine for contacts in the phone's address book. I am having a problem however when it comes to images from my google contacts.

Most of my google contacts do not have photos. However, when i query the Contacts database for photos, it still returns a URI for them of the form of content://com.android.contacts/contacts/657/photo (which is the same format as for contacts who do have a photo.

Then when I try to assign the photo to a QuickContactBadge, using bdg.setImageURI(pic); it sets it to essentially a blank picture, and logs a silent INFO message stating:

INFO/System.out(3968): resolveUri failed on bad bitmap uri: 
content://com.android.contacts/contacts/657/photo

I need to know how I can either
a) validate the URI or
b) catch the INFO message above
c) query the imageview/badge to see if it found a valid image

so that i can assign these contacts my "no image" image.
How can I go about doing this?

EDIT 20110812.0044

I have tried adding this to my code as per Laurence's suggestion (which he's since removed):

// rv is my URI variable
if(rv != null) {
    Drawable d = Drawable.createFromPath(rv.toString());
    if (d == null) rv = null;
}

While the google contacts now get my "no image" image, ... so do all the other contacts, including ones that do in fact have images.

like image 348
eidylon Avatar asked Aug 12 '11 04:08

eidylon


2 Answers

It could be that the images are not downloaded. I faced a similar problem with whatsapp images. One way to go about this could be like below:

InputStream is = null;
try {
   is = context.getContentResolver().openInputStream(myuri);
}catch (Exception e){
   Log.d("TAG", "Exception " + e);
}

if(is==null)
    //Assign to "no image"
like image 117
Bharath Kumar Avatar answered Oct 13 '22 07:10

Bharath Kumar


Okay, I figured out how to do this after poking through the ImageView source code. It is actually using the QuickContactBadge's own methods, but if necessary, one could always extract the relevant code from the Badge/ImageView control here.

After setting the QCB's image, I check to see if its drawable is null, instead of trying my own (as per Laurence's suggestion). This works better, because there is actually a whole slew of checking code the ImageView widget uses.

Here is my final code:

bdg.setImageURI(pic);
if(bdg.getDrawable() == null) bdg.setImageResource(R.drawable.contactg);

This works perfectly as I was hoping and expecting.

like image 36
eidylon Avatar answered Oct 13 '22 06:10

eidylon