2017-06-19 24 views
0

私は以下のコードを使用して、連絡先の電話番号と名前にアクセスしています。私はこの機能に連絡先の写真を追加する方法を追加したいと思います。電話連絡先から連絡先の写真を取得します。連絡先の写真をどのように進めるべきですか?

void loadContacts() { 
    ContentResolver contentResolver=getContentResolver(); 
    Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); 
    if(cursor.getCount() > 0) { 
     while (cursor.moveToNext()) {String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
      if (hasPhoneNumber > 0) { 
       Cursor cursor2 = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); 
       while (cursor2.moveToNext()) { 
        String phoneNumber = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
//make_contact() is a function that takes the contact details and adds them to a list 
//      make_contact(name,phoneNumber); 
       } 
       cursor2.close(); 
      } 
     } 
    } 
    cursor.close(); 
} 
+0

https://developer.android.com/reference/android/provider/ContactsContract.Contacts.Photo.html – RameshJaga

答えて

0

これは適切なXMLが存在すると仮定したサンプルアクティビティです。

package com.example;

import android.app.Activity; 
import android.content.ContentUris; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageView; 

import java.io.IOException; 
import java.io.InputStream; 

public class MyActivity extends Activity { 

    private static final String TAG = MyActivity.class.getSimpleName(); 
    private static final int REQUEST_CODE_PICK_CONTACTS = 1; 
    private Uri uriContact; 
    private String contactID;  // contacts unique ID 


    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void onClickSelectContact(View btnSelectContact) { 

     // using native contacts selection 
     // Intent.ACTION_PICK = Pick an item from the data, returning what was selected. 
     startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACTS); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) { 
      Log.d(TAG, "Response: " + data.toString()); 
      uriContact = data.getData(); 

      retrieveContactName(); 
      retrieveContactNumber(); 
      retrieveContactPhoto(); 

     } 
    } 

    private void retrieveContactPhoto() { 

     Bitmap photo = null; 

     try { 
      InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), 
        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID))); 

      if (inputStream != null) { 
       photo = BitmapFactory.decodeStream(inputStream); 
       ImageView imageView = (ImageView) findViewById(R.id.img_contact); 
       imageView.setImageBitmap(photo); 
      } 

      assert inputStream != null; 
      inputStream.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    private void retrieveContactNumber() { 

     String contactNumber = null; 

     // getting contacts ID 
     Cursor cursorID = getContentResolver().query(uriContact, 
       new String[]{ContactsContract.Contacts._ID}, 
       null, null, null); 

     if (cursorID.moveToFirst()) { 

      contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID)); 
     } 

     cursorID.close(); 

     Log.d(TAG, "Contact ID: " + contactID); 

     // Using the contact ID now we will get contact phone number 
     Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 

       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " + 
         ContactsContract.CommonDataKinds.Phone.TYPE + " = " + 
         ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, 

       new String[]{contactID}, 
       null); 

     if (cursorPhone.moveToFirst()) { 
      contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
     } 

     cursorPhone.close(); 

     Log.d(TAG, "Contact Phone Number: " + contactNumber); 
    } 

    private void retrieveContactName() { 

     String contactName = null; 

     // querying contact data store 
     Cursor cursor = getContentResolver().query(uriContact, null, null, null, null); 

     if (cursor.moveToFirst()) { 

      // DISPLAY_NAME = The display name for the contact. 
      // HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number. 

      contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     } 

     cursor.close(); 

     Log.d(TAG, "Contact Name: " + contactName); 

    } 
} 
関連する問題