2016-12-14 46 views
0

我想要一些照片插入到聯繫人的幫助,據我所研究,我發現了兩種方法,我們可以插入我們的手機中的聯繫人,一個是開始手機的聯繫人活動,另一種方法是直接在手機中插入值,我使用第一種方法,我們必須開始意圖,當我們開始意圖時,我沒有得到任何解決方案來添加圖像,我有選擇添加其他次要詳細信息如姓名,工作地點等。第二種方法的問題是,它不讓我們知道聯繫人是否已經添加,這可能會導致錯誤,它可能會創建重複的聯繫人。你建議我可以做什麼? 我在做什麼到現在是通過意圖將圖像傳遞到默認聯繫人活動

 Intent contactIntent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,ContactsContract.Contacts.CONTENT_URI); 
      contactIntent.setData(Uri.parse("tel:" +"+91"+mMobile)); 
      contactIntent.putExtra(ContactsContract.Intents.Insert.NAME, name); 
      contactIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, email); 
      contactIntent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, mobileEx); 
      startActivity(contactIntent); 
+1

嘗試通過意圖傳遞圖像的URI,而不是它自己 – thepaulo

回答

2

對於通過意圖傳遞輪廓影像聯繫人編輯屏幕,你可以這樣做如下圖所示

Intent contactIntent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,ContactsContract.Contacts.CONTENT_URI); 
    contactIntent.setData(Uri.parse("tel:" +"+91"+mMobile)); 
    contactIntent.putExtra(ContactsContract.Intents.Insert.NAME, name); 
    contactIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, email); 
    contactIntent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, mobileEx); 

    Bitmap bit = BitmapFactory.decodeResource(getResources(), R.drawable.profile_image); 

    ArrayList<ContentValues> data = new ArrayList<ContentValues>(); 

    ContentValues row = new ContentValues(); 
    row.put(Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE); 
    row.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bitmapToByteArray(bit)); 
    data.add(row); 
    contactIntent.putParcelableArrayListExtra(Insert.DATA, data); 
    startActivity(contactIntent); 

和邏輯轉換位圖的ByteArray是

private byte[] bitmapToByteArray(Bitmap bit) { 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bit.compress(Bitmap.CompressFormat.PNG, 100, stream); 
    byte[] byteArray = stream.toByteArray(); 
    return byteArray; 
} 
+0

你能給我btimaptobytearray函數嗎? –

+0

我已經更新了答案,請檢查它 –

+0

嘿,非常感謝你,這個作品! –

相關問題