我已经坚持这个问题好几天了,希望有人可以提供帮助。我正在尝试使用CONTACT_ID更新具有特定图像的联系人照片,但这似乎不起作用。我必须使用RAW_CONTACT_ID才能执行此操作吗?因为理想情况下我想将相同的照片应用于相同联系人ID的所有原始联系人组件。这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
//set up contact list
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter (this, android.R.layout.simple_list_item_2,
null, new String[] {Contacts.DISPLAY_NAME}, new int[] {android.R.id.text1}, 0);
ListView contactList = (ListView)findViewById(android.R.id.list);
contactList.setAdapter(mAdapter);
//set up intents for when a contact is selected
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Log.d(TAG, "action pick");
startActivityForResult(intent, PICK_CONTACT);
}
@Override
protected void onActivityResult (int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
super.onResume();
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
contactData = data.getData();
//get _ID (contact id based on first column in contact database
if (contactData != null) {
Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
if (cursor!=null && cursor.moveToNext()) {
contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Data._ID));
cursor.close();
}
}
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.woman);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
byte[] imageBytes = stream.toByteArray();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data._ID + " = ?", new String[]
{contactId}).withValue(ContactsContract.Data.DATA15,imageBytes).build());
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//view contact
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactId);
intent.setData(uri);
startActivity(intent);
break;
}
}
我尝试使用rawContactUri,但我不认为这与RAW_CONTACT_ID相同。有没有人对如何做到这一点有任何建议?
答案 0 :(得分:0)
调查一下,似乎PHOTO_ID是只读的(http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html),这表明我无法通过该程序对其进行更改。似乎其他人已经成功地做到了,所以如果有人找到方法让我知道!