1

I am trying to implement the example Android code given here: http://developer.android.com/reference/android/provider/ContactsContract.Contacts.Photo.html to grab the thumbnails of contacts (the top example code).

However, I can't get beyond the error: 'PHOTO cannot be resolved or is not a field'

This is the code:

public InputStream openPhoto(long contactId) 
    {
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
        Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
        Cursor cursor = getContentResolver().query(photoUri,
             new String[] {Contacts.Photo.PHOTO}, null, null, null);
        if (cursor == null) {
            return null;
        }
        try {
            if (cursor.moveToFirst()) {
                byte[] data = cursor.getBlob(0);
                if (data != null) {
                    return new ByteArrayInputStream(data);
                }
            }
        } finally {
            cursor.close();
        }
        return null;
    }

I have the following imports listed at the top of the class file:

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.Object;

import android.app.Activity;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

I am sure that it is something really simple, like a missing import, but I am at a loss. Any help would be much appreciated.

5
  • code seems fine. what is the device/emulator OS version you are testing on??
    – Varun
    Nov 1, 2011 at 11:57
  • This is showing up as an error in Eclipse, so I can't run it yet.
    – Swiss Bob
    Nov 1, 2011 at 12:00
  • 1
    In fact, I have tracked it down to an API level issue. It will stop reporting an error when the project is set to target Android 3 (API 11). It appears that the claim on the Android site that ContactsContract.Contacts.Photo has been available since API level 5 is untrue. Targeting Android 3 is no use to me unfortunately.
    – Swiss Bob
    Nov 1, 2011 at 12:06
  • yes. see my answer. you can use API 5. most of the android devices are well over Android 2.1(API 7) now
    – Varun
    Nov 1, 2011 at 12:09
  • 1
    I'm afraid that it doesn't work until the API level is set to 11 (Android 3) which few devices have yet. I was targeting it at API level 7 anyway. Even setting it at Android 2.3.3 - API 10 - still reports the error.
    – Swiss Bob
    Nov 1, 2011 at 12:20

2 Answers 2

1

Your target in eclipse should be >= API 5. make sure you target is Android 2.0 or above because the android.provider.ContactsContract.Contacts.Photo requires API 5 or greater

0

You should use

import android.provider.ContactsContract.Contacts;

instead of

import android.provider.Contacts;

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.