简体   繁体   中英

Android edit contact list with intent

I'm trying to edit a contact by intent. My code is :

Uri mUri = ContentUris.withAppendedId(Phone.CONTENT_URI,
                            Long.parseLong(mContact.str) );
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setDataAndType(mUri, Contacts.CONTENT_ITEM_TYPE);
intent.putExtra("finishActivityOnSaveCompleted", true);
mActivity.startActivityForResult(intent, EDIT_CONTACT_RESULT);

which mContact.str is contact's Contacts._ID . The code works on different devices with different Android versions. However I get the below error on Huawei 4.2.2 without any crash :

02-04 14:01:23.024: E/ContactLoader(8190): Error loading the contact: content://com.android.contacts/data/phones/22127
02-04 14:01:23.024: E/ContactLoader(8190): java.lang.IllegalArgumentException: uri format is unknown
02-04 14:01:23.024: E/ContactLoader(8190):  at com.android.contacts.util.ContactLoaderUtils.ensureIsContactUri(ContactLoaderUtils.java:64)
02-04 14:01:23.024: E/ContactLoader(8190):  at com.android.contacts.model.ContactLoader.loadInBackground(ContactLoader.java:425)
02-04 14:01:23.024: E/ContactLoader(8190):  at com.android.contacts.model.ContactLoader.loadInBackground(ContactLoader.java:82)
02-04 14:01:23.024: E/ContactLoader(8190):  at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:303)
02-04 14:01:23.024: E/ContactLoader(8190):  at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:68)
02-04 14:01:23.024: E/ContactLoader(8190):  at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:56)
02-04 14:01:23.024: E/ContactLoader(8190):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-04 14:01:23.024: E/ContactLoader(8190):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-04 14:01:23.024: E/ContactLoader(8190):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-04 14:01:23.024: E/ContactLoader(8190):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-04 14:01:23.024: E/ContactLoader(8190):  at java.lang.Thread.run(Thread.java:838)

Also I'm using READ_CONTACTS and WRITE_CONTACTS permissions on my Manifest file.

  Uri mUri = ContentUris.withAppendedId(Phone.CONTENT_URI,
                            Long.parseLong(mContact.str) );
  Intent intent = new Intent(Intent.ACTION_EDIT);
  intent.setDataAndType(mUri, Contacts.CONTENT_ITEM_TYPE);
  intent.putExtra("finishActivityOnSaveCompleted", true);
  //add the below line 
  intent.addFlag(intent.FLAG_ACTIVITY_CLEAR_TOP);
  mActivity.startActivityForResult(intent, EDIT_CONTACT_RESULT);

Ok I finally found the solution ! The URI should be like this :

Uri mUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
                                Long.parseLong(mContact.str));

Also mContact.str should be like below:

mContact.str = phones.getString(phones
                            .getColumnIndex(Phone._ID));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM