2013-12-20 58 views
1

在android我想獲取聯繫人數據,我得到了號碼成功,但聯繫人照片我面臨問題。我得到了聯繫人照片URI content://com.android.contacts/data/6376/photo但是當我將它設置爲圖像視圖,圖像視圖將空白android:獲取聯繫人照片不工作,嘗試了很多東西,但仍然沒有工作

/*getting activity result */ 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 1) { 
     // return from file upload 
     if (resultCode == Activity.RESULT_OK) { 
      if (data != null) { 
       Uri uri = data.getData(); 
       String contactID=""; 
       if (uri != null) { 
       Cursor c = null; 
        try { 
         c = getContentResolver().query(uri, new String[]{ 
           ContactsContract.CommonDataKinds.Phone.NUMBER, 
           ContactsContract.CommonDataKinds.Phone.TYPE, 
           ContactsContract.Contacts._ID 
           //ContactsContract.CommonDataKinds.Phone.p 
          }, null, null, null); 

         if (c != null && c.moveToFirst()) { 
          String number = c.getString(0); 
          int type = c.getInt(1); 
          contactID = c.getString(2); 

          Bitmap photoBitmap = null; 
          Uri photo=null; 
          try { 
           photo = Uri.withAppendedPath(uri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
           ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
           imageView.setImageURI(photo); 

//        ImageView profile = (ImageView)findViewById(R.id.imageView1);     
//        Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactID); 
//        InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), my_contact_Uri);    
//        BufferedInputStream buf = new BufferedInputStream(photo_stream); 
//        Bitmap my_btmp = BitmapFactory.decodeStream(buf); 
//        profile.setImageBitmap(my_btmp); 
          } catch (Exception e) { 
           e.printStackTrace(); 
          } 

          // contactNumber = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.NUMBER))); 
          showSelectedNumber(type, number,contactID,photo.toString()); 

         } 




//       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.imageView1); 
//         imageView.setImageBitmap(photo); 
//        } 
//      
//        assert inputStream != null; 
//        inputStream.close(); 
//      
//       } catch (IOException e) { 
//        e.printStackTrace(); 
//       } 


          }catch(Exception e){ 
           Log.w(TAG,e+""); 

          } finally { 
           if (c != null) { 
            c.close(); 
           } 
          } 
         } 
        } 

       } else { 
        Log.w(TAG, "Unknown Activity Result from add contact: " 
          + resultCode); 
       } 
      } 
    } 

在上面的代碼我試過很多東西,比如輸入流等,但仍然沒有得到聯繫人的照片但成功獲取照片路徑。

當我要在圖像視圖上設置數據時,它不會顯示任何內容。

+0

看看我的回答,並相應地嘗試。 – GrIsHu

回答

0

如果你已經擁有的路徑,你可以試試這個:

private void previewImage(String path) { 
    try { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 5; // setting the photo quality 
     final Bitmap bitmap = BitmapFactory.decodeFile(path, options); 
     photo.setImageBitmap(bitmap); 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } 
} 
+0

不工作,我認爲有URI路徑的問題 –

0

檢索縮略圖遵循這個代碼..

public InputStream openPhoto(long contactId) { 
    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); 
    Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY); 
    Cursor cursor = getContentResolver().query(photoUri, 
      new String[] {Contacts.Photo.PHOTO}, null, null, null); 
    if (cursor == null) { 
     return null; 
    } 
    try { 
     if (cursor.moveToFirst()) { 
      byte[] data = cursor.getBlob(0); 
      if (data != null) { 
       return new ByteArrayInputStream(data); 
      } 
     } 
    } finally { 
     cursor.close(); 
    } 
    return null; 
} 
+0

我試過你的代碼,但沒有工作。 Contacts.Photo.PHOTO不可用。 –

0

嘗試下面的代碼它會幫助你取得聯繫號碼,它的ID和來自該ID號的它將獲取聯繫號碼。

public class DemoContact extends Activity { 

    private static final String TAG = DemoContact.class.getSimpleName(); 
    private static final int REQUEST_CODE_PICK_CONTACTS = 1; 
    private Uri uriContact; 
    private String contactID, contactName; // contacts unique ID 
    private TextView m_contactName; 
    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
      m_contactName = (TextView) findViewById(R.id.textView1); 
    } 

    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(); 
      retrieveContactNumber(); 
      retrieveContactPhoto(); 
       m_contactName.setText("Contact Name: "+contactName); 

     } 
    } 
    //Retrieve the Contact photo based on the contactId 
    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(); 
     } 

    } 
    //Get the contact number of the selected contact. 
    private void retrieveContactNumber() { 

     String contactNumber = null; 

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

     if (cursorID.moveToFirst()) { 

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

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

輸出:

enter image description here

+0

它也沒有工作... –

+0

@dineshaggarwal看看我更新的答案。它的工作現在很好。 – GrIsHu

相關問題