资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

android怎么取得本地通讯录的头像的原图

这篇文章将为大家详细讲解有关android怎么取得本地通讯录的头像的原图,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联主要从事成都网站制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务西城,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

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

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();
   }
  }
});

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

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 {
 
    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怎么取得本地通讯录的头像的原图

关于“android怎么取得本地通讯录的头像的原图”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


本文名称:android怎么取得本地通讯录的头像的原图
分享URL:http://cdkjz.cn/article/geehco.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220