15

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.

3
  • Do you know where the contact images are stored on the SDcard/internal memory? Maybe if you could locate the folder, then iterating through all the member you could validate if the selected image exist or not. Aug 12, 2011 at 7:46
  • Well, the only URI I get is the content://com.android.contacts/... URI mentioned, which is simply referring to a field in the Contacts2.db file in the db store. Looking at the PHOTOS view in SQLLite Editor, it looks like the photos are stored directly in the db as raw binary data (the data field doesn't show a path, but just shows (data)). One of the contacts in question does NOT show (data) for their photo, but shows an empty field. Is there somehow I can query the URI to see if it returns NULL?
    – eidylon
    Aug 12, 2011 at 15:05
  • The real question is: why are those fake photo records are stored in the database at all?
    – xnagyg
    Jan 1, 2015 at 20:44

6 Answers 6

15

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.

8
  • This is fine, but if you use an Adapter, you can not check this way. So you should check the uri before
    – xnagyg
    Jan 1, 2015 at 20:48
  • this is really bad, especially when in a grid or list view.
    – Olayinka
    May 7, 2015 at 12:47
  • @Olayinka - care to expound a little more and add to the conversation? "It's bad." doesn't really explain much. How should it be done then?
    – eidylon
    May 9, 2015 at 22:30
  • @eidylon It's bad because in a list view where multiple items are loaded new bitmaps are created everytime you call setImageUri which takes time and memory.
    – Olayinka
    May 9, 2015 at 22:41
  • Well the gridview does actually have a QuickContactBadge in the item template. I would expect of Google that the QCB would reuse the same bitmap. So either way, whether it uses the one from the contact or whether it winds up using my "no image" image, it still is going to be creating a bitmap anyway. I'm not creating a QCB just to test the image.
    – eidylon
    May 9, 2015 at 22:46
4

Just to answer the question on how to check the (data) value in the MediaStore:

ContentResolver cr = getContentResolver();
String[] projection = {MediaStore.MediaColumns.DATA}
Cursor cur = cr.query(Uri.parse(contentUri), projection, null, null, null);
if(cur != null) {
    cur.moveToFirst();
    String filePath = cur.getString(0);

    if (filePath == null || filePath.isEmpty()) {
        // data not set
    } else if((new File(filePath)).exists()){
        // do something if it exists
    } else {
        // File was not found
        // this is binary data
    }
} else {
    // content Uri was invalid or some other error occurred 
}

Inspiration taken from: https://stackoverflow.com/a/7649784/621690 and others.

There is also the column SIZE that might be checked: http://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html#SIZE It sounds like it should contain 0 if there is no data value. But I wouldn't know what it contains if data is a file path.

3

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"
0

Based on the code (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/ImageView.java) my solution for checking Uri:

public static Uri checkUriExists (Context  mContext,Uri mUri) {
    Drawable d = null;
    if (mUri != null) {
        if ("content".equals(mUri.getScheme())) {
            try {
                d = Drawable.createFromStream(
                        mContext.getContentResolver().openInputStream(mUri),
                        null);
            } catch (Exception e) {
                Log.w("checkUriExists", "Unable to open content: " + mUri, e);
                mUri = null;
            }
        } else {
            d = Drawable.createFromPath(mUri.toString());
        }

        if (d == null) {
            // Invalid uri
            mUri = null;
        }
    }

    return mUri;
}
1
  • Its works, but it's too slowly. Jan 27, 2022 at 12:27
0

I created a function which returns boolean

private fun isUriValid(context: Context,uri: Uri):Boolean{
    val contentResolver = context.contentResolver
    return try {
        contentResolver.openInputStream(uri)?.close()
        true
    } catch (e:Exception){
        return false
    }
}
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 6, 2023 at 14:37
-1

I am using this code for Uri that has file:// authority

Uri resimUri = Uri.parse(path_str);
File imgFile = new File(resimUri.getPath());
if (imgFile.exists()) {
    // file exists
}else {
    // file is not there
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.