0

私は単にすべての連絡先リストをオブジェクト (PhoneBookContact) に取得しています:

 private static final Uri                         PURI      = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
 private static final String                      PID       = ContactsContract.CommonDataKinds.Phone._ID;
 private static final String                      PNAME     = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
 private static final String                      PNUM      = ContactsContract.CommonDataKinds.Phone.NUMBER;


 String[] projection = new String[]{PID, PNAME, PNUM};

 String sortOrder = PNAME + " COLLATE LOCALIZED ASC";

 Cursor people = mContext.getContentResolver().query(PURI, projection, null, null, sortOrder);

 int indexid = people.getColumnIndex(PID);
 int indexName = people.getColumnIndex(PNAME);
 int indexNumber = people.getColumnIndex(PNUM);

 people.moveToFirst();
 do {
    PhoneBookContact phoneBookContact = new PhoneBookContact();
    phoneBookContact.setmCursorId(people.getLong(indexid));
    phoneBookContact.setmDisplayName(people.getString(indexName));
    phoneBookContact.setmPhoneNumber(UriFactory.formatNumberToInternational(people.getString(indexNumber)));
    phoneList.put(phoneBookContact.getmPhoneNumber(), phoneBookContact);
 } while (people.moveToNext());
 people.close();

Android の連絡先からユーザーの写真を取得するために、次の方法を使用しています。

   public static Uri loadContactPhotoUri(ContentResolver contentResolver, long id) {
      try {
         Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
         return person;
      }
      catch (Exception e) {
         e.printStackTrace();
         return null;
      }
   }

最後に、このコードは特定のユーザーに次の URI を提供します。

content://com.android.contacts/contacts/5907

ContactsContract.CommonDataKinds.Phone._ID id でこの URI を呼び出しているため、ユーザーの写真を正常に取得できません。実際、これは ContactsContract.Contacts._ID の ID である必要があります。ユーザーの写真を照会してこの問題を解決するにはどうすればよいですか?

4

2 に答える 2

0

このファイルには、連絡先の写真を取得する機能があります

https://github.com/heinrisch/Contact-Picture-Sync/blob/master/src/heinrisch/contact/picture/sync/ContactHandler.java

package heinrisch.contact.picture.sync;

import java.io.InputStream;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDiskIOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts.Data;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;

public class ContactHandler {


    public static ArrayList<String> getNumbers(String ID, Context context){
        ArrayList<String> numbers = new ArrayList<String>();
        ContentResolver cr = context.getContentResolver();
        Cursor phones = cr.query(Phone.CONTENT_URI, null,Phone.CONTACT_ID + " = " + ID, null, null);
        while (phones.moveToNext()) {
            numbers.add(phones.getString(phones.getColumnIndex(Phone.NUMBER)));
        }
        phones.close();

        return numbers;

    }

    public static void matchContactsToFriends(ArrayList<Friend> friends, Context context) {
        Cursor people = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if(people == null){
            Log.e("ContactHandler", "Could not find contacts...?");
            return;
        }
        while(people.moveToNext()) {
            String ID = null,name = null;
            int columnIndex = people.getColumnIndex(ContactsContract.Contacts._ID);
            if(columnIndex != -1) ID = people.getString(columnIndex);
            columnIndex = people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            if(columnIndex != -1) name = people.getString(columnIndex);

            //ArrayList<String> numbers = getNumbers(ID,context); //"can't" get this from facebook
            if(name == null) continue;

            for(Friend f : friends){
                if(f.isMatchedWithContact()) continue;
                if(f.getName().equals(name)){
                    f.setContactID(ID);
                    Bitmap contactPhoto = getPhoto(context, ID);
                    if(contactPhoto != null) f.setContactPicture(contactPhoto);
                    break;
                }
            }

        }
        people.close();
    }


    public static void setContactPicture(Friend f, Context context){
        f.setContactPicture(getPhoto(context, f.getContactID()));
    }


    public static int getNumberOfContacts(Context context) {
        Cursor people = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        int numberOfContacts = people.getCount();
        people.close();
        return numberOfContacts;
    }


    public static Uri getPicture(Context context, String ID){
        ContentResolver cr = context.getContentResolver();
        Uri rawContactUri = null;
        Cursor rawContactCursor =  cr.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID}, RawContacts.CONTACT_ID + " = " + ID, null, null);
        if(!rawContactCursor.isAfterLast()) {
            rawContactCursor.moveToFirst();
            rawContactUri = RawContacts.CONTENT_URI.buildUpon().appendPath(""+rawContactCursor.getLong(0)).build();
        }
        rawContactCursor.close();

        return rawContactUri;
    }


    public static void setContactPicture(Context context, String ID, Bitmap picture){
        ContentResolver cr = context.getContentResolver();
        Uri rawContactUri = getPicture(context, ID);
        if(rawContactUri == null){
            Log.e("rawContactUri", "is null");
            return;
        }
        ContentValues values = new ContentValues(); 
        int photoRow = -1; 
        String where = ContactsContract.Data.RAW_CONTACT_ID + " == " + 
                ContentUris.parseId(rawContactUri) + " AND " + Data.MIMETYPE + "=='" + 
                ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'"; 
        Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, null, where, null, null); 
        int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID); 
        if(cursor.moveToFirst()){ 
            photoRow = cursor.getInt(idIdx); 
        } 
        cursor.close(); 
        values.put(ContactsContract.Data.RAW_CONTACT_ID, 
                ContentUris.parseId(rawContactUri)); 
        values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1); 
        values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, Tools.bitmapToByteArray(picture)); 
        values.put(ContactsContract.Data.MIMETYPE, 
                ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE); 
        try{
            if(photoRow >= 0){ 
                cr.update(
                        ContactsContract.Data.CONTENT_URI, 
                        values, 
                        ContactsContract.Data._ID + " = " + photoRow, null);
            } else { 
                cr.insert(
                        ContactsContract.Data.CONTENT_URI, 
                        values); 
            } 
        }catch(SQLiteDiskIOException dIOe){
            //TODO: should show this to the user..
            dIOe.printStackTrace();
        }
    } 

    public static Bitmap getPhoto(Context context, String contactId) {
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
        if (input == null) {
            return null;
        }
        return BitmapFactory.decodeStream(input);
    }
}
于 2015-11-17T08:59:00.473 に答える
0

ContactsContract.CommonDataKinds.Phone._ID の代わりに、ContactsContract.CommonDataKinds.Phone.CONTACT_ID を使用して Contacts テーブルの cursorID を保存する必要があります。それは私の問題を解決しました。

于 2013-01-22T12:53:07.367 に答える