標籤:通訊錄 頭像清晰圖
上幾篇講contact的時候,取得的頭像都是存在資料庫中,通過:
cursor = context.getContentResolver().query( ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId), new String[] { Photo.PHOTO }, null, null, null);
來擷取
其實如果我們在本地通訊錄中,如果替一個連絡人加庫或拍照的圖片,那麼系統會把這個圖片的清晰圖儲存起來,如果要取到這個圖片,需要contactId
getListView().setOnItemClickListener(new OnItemClickListener() {@Overridepublic 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代碼:
@Overrideprotected 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;@Overrideprotected 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;}@Overrideprotected 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; } }
: