首页 > 代码库 > android如何取得本地通讯录的头像的原图

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

上几篇讲contact的时候,取得的头像都是存在数据库中,通过:

 cursor = context.getContentResolver().query(  
                    ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId),  
                    new String[] { Photo.PHOTO }, null, null, null);  

来获取


其实如果我们在本地通讯录中,如果替一个联系人加上图库或拍照的图片,那么系统会把这个图片的清晰图存储起来,如果要取到这个图片,需要contactId

getListView().setOnItemClickListener(new 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();
				}
				
			}
		});


如果我们想图片带圆角,可以这样来实现:

     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >
        
        <FrameLayout
            android:id="@+id/contacts_logo_layout"
            android:layout_width="@dimen/incall_photo_view_size"
            android:layout_height="@dimen/incall_photo_view_size" >

            <ImageView
                android:id="@+id/contact_photo"
                android:layout_width="@dimen/incall_photo_view_size"
                android:layout_height="@dimen/incall_photo_view_size"
                />

            <ImageView
                android:id="@+id/contacts_logoborder"
                android:layout_width="@dimen/incall_photo_view_size"
                android:layout_height="@dimen/incall_photo_view_size"
                android:background="@drawable/images_box" >
            </ImageView>
        </FrameLayout>
        
    </LinearLayout>

java代码:

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

		
	}
	
	public InputStream openDisplayPhoto(long contactId) {  <strong>//这个是取到清晰图的inputStream的代码</strong>
	     Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
	     Uri displayPhotoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.DISPLAY_PHOTO);
	     try {
	         AssetFileDescriptor fd =
	        		this.getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
	         return fd.createInputStream();
	     } catch (IOException e) {
	    	 e.printStackTrace();
	         return null;
	     }
	 }

效果图: