2011-09-30 109 views
0

我正在处理联系人的android应用程序。代码在Android 2.1中不起作用

我在android 1.6中使用了下面的代码,它工作正常。

public static Uri getProfilepicture(Activity activity, String address) 
{ 
    Uri personUri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, address); 
    Cursor phoneCursor = activity.getContentResolver().query(personUri,PHONE_PROJECTION, null, null, null); 
    if (phoneCursor.moveToFirst()) 
    { 
     int indexPersonId = phoneCursor.getColumnIndex(Phones.PERSON_ID); 
     long personId = phoneCursor.getLong(indexPersonId); 
     phoneCursor.close(); 

     Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, personId); 
     return uri; 
    } 
    return null; 
} 

并获得照片的位图一样

Bitmap bm = People.loadContactPhoto(activity,getProfilepicture(activity, ConNum, R.drawable.artist, null); 

任何一个可以建议为Android 2.1的代码吗?

+0

People'此课程已弃用。 请参阅ContactsContract' – Selvin

+0

感谢评论selvin。我知道我必须使用ContactsContract。在api 2.1我试着用它,但没有得到输出。如果你有任何工作的例子,请提供给我。 – milind

+1

@milind我认为[这里](http://mobile.tutsplus.com/tutorials/android/android-essentials-using-the-contact-picker/)是使用ContactsContract ....的罕见例子。您可能已经看到。 – Hanry

回答

1

感谢朋友试图帮助我。我通过下面的代码解决了这个问题。

public static Bitmap getContactPhoto(Activity activity,int contactId) 
{ 
    Bitmap photo = null; 

    final String[] projection = new String[] { 
      Contacts.PHOTO_ID      // the id of the column in the data table for the image 
    }; 

    final Cursor contact = activity.managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,projection,Contacts._ID + "=?",new String[]{String.valueOf(contactId)},null); 

    if(contact.moveToFirst()) 
    { 
     final String photoId = contact.getString(
       contact.getColumnIndex(Contacts.PHOTO_ID)); 
     if(photoId != null) 
     { 
      photo = queryContactBitmap(activity,photoId); 
     } 
     else 
     { 
      photo = null; 
     } 
     contact.close(); 

    } 
    contact.close(); 
    return photo; 
} 


private static Bitmap queryContactBitmap(Activity activity,String photoId) 
{ 
    final Cursor photo = activity.managedQuery(Data.CONTENT_URI,new String[] {Photo.PHOTO},Data._ID + "=?",new String[]{photoId},null); 

    final Bitmap photoBitmap; 
    if(photo.moveToFirst()) 
    { 
     byte[] photoBlob = photo.getBlob(
       photo.getColumnIndex(Photo.PHOTO)); 
     photoBitmap = BitmapFactory.decodeByteArray(
       photoBlob, 0, photoBlob.length); 
    } 
    else 
    { 
     photoBitmap = null; 
    } 
    photo.close(); 
    return photoBitmap; 
} 

在那只是传递活动对象和contactId。并将其存储到bitmam中。

+0

谢谢米勒。它的作品像我的魅力。 –