How does android obtain the source image of the local address book?

Source: Internet
Author: User

How does android obtain the source image of the local address book?

When talking about the contact in the previous articles, all the headers obtained are stored in the database:

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

To obtain


In fact, if we add a library or a picture to a contact in the Local Address Book, the system will store the clear picture of the picture. If we want to get the picture, we need 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();}}});


If you want the image to have rounded corners, you can do this:

     
 
                  <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 code:

@ 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.exe cute ();} private class loadTask extends AsyncTask
 
  
{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 ){
  // This is the inputStream code that obtains the clear graph.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 ;}}
 

:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.