Adding the user's contact's photo from phone to ListView? :D

核能气质少年 提交于 2019-12-11 07:55:04

问题


I have created a list of contact's in user phone, now i want to add the user's photo (not from fb) how to create that? :D This is a piece of my code :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(Uri.parse("content://sms/inbox"), null, null,
            null, null);

    int indexBody = cursor.getColumnIndex("body");
    int indexAddr = cursor.getColumnIndex("address");

    if (indexBody < 0 || !cursor.moveToFirst())
        return;

    smsList.clear();

    do {
        String str = "Sender : " + cursor.getString(indexAddr) + "\n"
                + cursor.getString(indexBody);
        smsList.add(str);
        // ADDRESS[total] = cursor.getString(indexAddr);
        // total++;
    } while (cursor.moveToNext());

    ListView lvSms = (ListView) findViewById(R.id.SMSList);
    lvSms.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, smsList));

    // cursor.requery();
    lvSms.setOnItemClickListener(this);
}

I create the list in this code :

do {
        String str = "Sender : " + cursor.getString(indexAddr) + "\n"
                + cursor.getString(indexBody);
        smsList.add(str);
        // ADDRESS[total] = cursor.getString(indexAddr);
        // total++;
    } while (cursor.moveToNext());

    ListView lvSms = (ListView) findViewById(R.id.SMSList);
    lvSms.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, smsList));

    // cursor.requery();
    lvSms.setOnItemClickListener(this);

Thats all, i found that i must use Bitmap, is it true? Thanks all :D

PS : English is not my native languange, so sorry if i made some mistake's :D


回答1:


Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
                contactId);
        Uri photoUri = Uri.withAppendedPath(contactUri,
                Contacts.Photo.CONTENT_DIRECTORY);
        Cursor cursor = getContentResolver().query(photoUri, null, null, null,
                null);

using this query you can fetch the image url for your user photos in your contact list, in order to display along with name and number in list , you have to Base or any other custom adapter.



来源:https://stackoverflow.com/questions/9513781/adding-the-users-contacts-photo-from-phone-to-listview-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!