android如何取得本地通讯录的头像的原图

如果想通讯录进入详情页,那么最重要的参数就是contactId,这个是联系人的唯一标识

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

   @Override

   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

      try {

         Cursor cursor = (Cursor) getListAdapter().getItem(position);

         if (cursor == null) {

            return;

         }

         int contactId = cursor.getInt(Personal.ID_COLUMN_INDEX);

         Intent intent = new Intent();

         intent.setClass(ContactsList.this, ContactDetail.class);

         intent.putExtra("contactId",contactId);

         startActivity(intent);

      }catch(Exception ex) {

         ex.printStackTrace();

      }

   }

});

本地通讯录的原图获取方法是:

>注‘Android技术交流群878873098,欢迎大家加入交流,畅谈!本群有免费学习资料视频且免费分享

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

public class ContactDetail extends Activity {

    private ImageView contact_photo;

    public static void startActivity(Context context) {

        Intent intent = new Intent();

        intent.setClass(context,ContactDetail.class);

        context.startActivity(intent);

    }

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.contact_detail);

        int contactId = getIntent().getIntExtra("contactId"0);

        contact_photo = (ImageView)findViewById(R.id.contact_photo);

        loadTask task = new loadTask(contactId);

        task.execute();

    }

    private class loadTask extends AsyncTask<Void, Void, Bitmap> {

        public loadTask(int id) {

            contactId = id;

        }

        private int contactId;

        @Override

        protected Bitmap doInBackground(Void... params) {

            InputStream inputStream = openDisplayPhoto(contactId);

            BitmapFactory.Options opt = new BitmapFactory.Options();

            opt.inSampleSize = 1;

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, opt);

            return bitmap;

        }

        @Override

        protected void onPostExecute(Bitmap result) {

            if(result != null) {

                contact_photo.setImageBitmap(result);

            }

            super.onPostExecute(result);

        }

    }

    /**

     * 这个是取到清晰图的inputStream的代码

     * @param contactId

     * @return

     */

    public InputStream openDisplayPhoto(long contactId) {

        Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);

        Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);

        try {

            AssetFileDescriptor fd =

                    this.getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");

            return fd.createInputStream();

        catch (IOException e) {

            e.printStackTrace();

            return null;

        }

    }

}

activity的运行效果是:

>注‘Android技术交流群878873098,欢迎大家加入交流,畅谈!本群有免费学习资料视频且免费分享

https://img3.mukewang.com/5c0e136d00013d8306481106.jpg


 

猜你喜欢

转载自blog.csdn.net/qq_43093708/article/details/85731973