Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android如何取得本地通訊錄的頭像的原圖

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


如果我們想圖片帶圓角,可以這樣來實現:

     
        
        <frameLayout
            android:id="@+id/contacts_logo_layout"
            android:layout_width="@dimen/incall_photo_view_size"
            android:layout_height="@dimen/incall_photo_view_size" >

            

            
            
        </frameLayout>
        
    

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{
		
		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) {  //這個是取到清晰圖的inputStream的代碼
	     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;
	     }
	 }

效果圖:


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved