2013-07-22 66 views
4

我有一張圖片,它存儲在android手機中。我希望能夠改變聯繫人的圖片。以編程方式更改聯繫人圖片

我到目前爲止所做的是啓動聯繫人選擇器,讓用戶選擇一個聯繫人,然後獲取所選聯繫人的URI。從這個聯繫人中,我可以獲得關聯的rawContact,並使用this code

Uri rawContactPhotoUri = Uri.withAppendedPath(
      ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), 
      RawContacts.DisplayPhoto.CONTENT_DIRECTORY); 
    try { 
     AssetFileDescriptor fd = 
      getContentResolver().openAssetFileDescriptor(rawContactPhotoUri, "rw"); 
     OutputStream os = fd.createOutputStream(); 
     os.write(photo); 
     os.close(); 
     fd.close(); 
    } catch (IOException e) { 
     // Handle error cases. 
    } 

問題是,AssetFIleDescriptor總是空的(當我調用它的長度時,我們總是得到-1)。

我不是要求整個解決方案,只是一些導致跟隨,可以幫助我得到那個工作。我似乎無法在StackOverflow上找到這個問題,所以任何幫助,將不勝感激。

編輯

它總是當我們問對問題,我們找到解決方案。 我想分享其他

所以我放棄了在Android上的鏈接,找到另一個問題: http://wptrafficanalyzer.in/blog/programatically-adding-contacts-with-photo-using-contacts-provider-in-android-example/

圖片選擇器返回選定聯繫人的URI,所以這個你可以得到的聯繫。它_ID:

// This is onActivityResult 
final Uri uri = data.getData(); 
final Cursor cursor1 = getContentResolver().query(uri, null, null, null, null); 
cursor.moveToFirst(); 
final long contactId = cursor1.getLong(cursor1.getColumnIndex(Contacts._ID); 
cursor1.close(); 

然後我不得不讓RawContactId:

final Cursor cursor2 = getContentResolver().query(RawContacts.CONTENT_URI, null,  RawContacts.Contact_ID + "=?", new String[] {String.valueOf(contactId)}, null); 
cursor2.moveToFirst(); 
final long rawContactId = cursor2.getLong(cursor2.getColumnIndex(RawContacts._ID)); 
cursor2.close(); 

然後我必須獲得RawContacts的Data._ID(與上面相同)。

然後我用了ContentProviderOperations:

final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
    .withSelection(Data._ID, dataId), 
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) 
    .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArrayOfThePicture); 

getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 

而且這是工作般的魅力。希望它有幫助

+0

你在AndroidManifest.xml中有什麼權限?聯繫人選擇器對檢索到的URI提供臨時READ權限,但您需要設置WRITE_CONTACTS權限才能實際更新聯繫人。 – Josiah

+0

我有READ和WRITE_CONTACT權限。 – Tristan

回答

1
String contactId ="10001"; // change it as your IDs 
    if (mBitmap != null) { 
       // Picture 
       try { 
        ByteArrayOutputStream image = new ByteArrayOutputStream(); 
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, image); 

        Uri rawContactUri = null; 
        Cursor rawContactCursor = managedQuery(
          ContactsContract.RawContacts.CONTENT_URI, 
          new String[]{ContactsContract.RawContacts._ID}, 
          ContactsContract.RawContacts.CONTACT_ID + " = " + contactId, 
          null, 
          null); 
        if (!rawContactCursor.isAfterLast()) { 
         rawContactCursor.moveToFirst(); 
         rawContactUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendPath("" + rawContactCursor.getLong(0)).build(); 
        } 
        rawContactCursor.close(); 

        ContentValues values = new ContentValues(); 
        int photoRow = -1; 
        String where111 = ContactsContract.Data.RAW_CONTACT_ID + " == " + 
          ContentUris.parseId(rawContactUri) + " AND " + ContactsContract.Data.MIMETYPE + "=='" + 
          ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'"; 
        Cursor cursor = managedQuery(
          ContactsContract.Data.CONTENT_URI, 
          null, 
          where111, 
          null, 
          null); 
        int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID); 
        if (cursor.moveToFirst()) { 
         photoRow = cursor.getInt(idIdx); 
        } 

        cursor.close(); 


        values.put(ContactsContract.Data.RAW_CONTACT_ID, 
          ContentUris.parseId(rawContactUri)); 
        values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1); 
        values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray()); 
        values.put(ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE); 
        if (photoRow >= 0) { 
         getContentResolver().update(
           ContactsContract.Data.CONTENT_URI, 
           values, 
           ContactsContract.Data._ID + " = " + photoRow, null); 
        } else { 
         getContentResolver().insert(
           ContactsContract.Data.CONTENT_URI, 
           values); 
        } 
       } catch (Exception e) { 
        Log.e("[email protected]@Image_Exception", e + ""); 
       } 
      } 
try { 
      getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
     } catch (Exception e) { 
      Log.e("@@@@@UPLOADERR", e + ""); 
     } 
相關問題