Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 取得本地通訊錄聯絡人頭像原圖

編輯:關於 Android 程式設計

先前介紹聯絡人(Contact)時,頭像多半是從資料庫取得,方式如下:

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

其實若在本地通訊錄中為聯絡人加上圖庫或拍照的圖片,系統會另外存一份清晰的原圖。要取得這張原圖,需要 contactId。

在清單中點擊項目時取得 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 疊加邊框 ImageView 來實現:

<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" /> </FrameLayout> </LinearLayout>

Java 程式碼以 AsyncTask 在背景執行緒載入原圖並設定到 ImageView:

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

其中 openDisplayPhoto 即為取得清晰原圖 inputStream 的關鍵程式碼。成果如下:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved