以下是我的代码,但是总是添加的时候程序会崩溃,也没有错误的日志,麻烦哪位大神帮忙一下
ContentValues values = new ContentValues();
//先向RawContact.CONTENT_URI执行一个控制插入,目的是获得系统返回的rawContacctId
Uri rawContactUri = this.getApplicationContext().getContentResolver() .insert(ContactsContract.RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
//往data表里写入姓名数据
values.clear(); values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); //内容类型 values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, information[0]); this.getApplicationContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
android中如何向系统中添加联系人数据
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
你知我知皆知 2024-08-04 09:54关注以下回答参考 皆我百晓生、券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。
在Android中向系统中添加联系人数据,可以使用ContentProvider的方式。下面是一个基本的例子:
public class ContactProvider extends ContentProvider { private static final String CONTENT_AUTHORITY = "com.example.android.contacts"; private static final String DATABASE_NAME = "contacts.db"; private static final int DATABASE_VERSION = 1; private static final Uri CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY + "/"); @Override public boolean onCreate() { return true; } @Nullable @Override public Cursor query(@NonNull Uri uri, @NonNull String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Nullable @Override public String getType(@NonNull Uri uri) { throw new UnsupportedOperationException("Not supported yet."); } @Nullable @Override public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) { if (isExternalStorageDocument(uri)) { return insertFiles(uri, values); } if (isDownloadsDocument(uri)) { return insertDownloadFile(uri, values); } if (isMediaDocument(uri)) { return insertMediaFile(uri, values); } if (uri.getLastPathSegment().equals("_rels")) { return insertRels(uri, values); } return null; } private Uri insertFiles(Uri uri, ContentValues values) { // Add the file to the media store with a unique name String filePath = getFilePathFromUri(uri); File contactFile = new File(filePath); if (!contactFile.exists()) { throw new FileNotFoundException(); } return super.insert(uri, values); } private Uri insertDownloadFile(Uri uri, ContentValues values) { // Add the downloaded file to the media store with a unique name String downloadPath = getFilePathFromUri(uri); File contactFile = new File(downloadPath); if (!contactFile.exists()) { throw new FileNotFoundException(); } return super.insert(uri, values); } private Uri insertMediaFile(Uri uri, ContentValues values) { // Add the downloaded file to the media store with a unique name String mediaPath = getFilePathFromUri(uri); File contactFile = new File(mediaPath); if (!contactFile.exists()) { throw new FileNotFoundException(); } return super.insert(uri, values); } private Uri insertRels(Uri uri, ContentValues values) { // Add the downloaded file to the media store with a unique name String relsPath = getFilePathFromUri(uri); File contactFile = new File(relsPath); if (!contactFile.exists()) { throw new FileNotFoundException(); } return super.insert(uri, values); } /** * Returns whether or not the given URI is for an external storage location. */ private boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * Returns whether or not the given URI is for a download. */ private boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * Returns whether or not the given URI is for a media document. */ private boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } private String getFilePathFromUri(Uri uri) { String filePath; if ("_rels".equals(uri.getLastPathSegment())) { filePath = uri.getPath().substring(0, uri.getPath().lastIndexOf('/')); } else { filePath = uri.getPath(); } return filePath; } }在这个例子中,我们创建了一个名为
ContactProvider的ContentProvider类,它继承自ContentProvider。然后我们在onCreate方法中重写了createCursor,getType和insert方法。onCreate方法被重写为返回true,以表示这个ContentProvider可以用于创建新的数据库。query方法被重写为抛出异常,因为我们不支持查询操作。insert方法被重写为处理不同的URI(文件、下载或媒体)并返回正确的Uri。
解决 无用评论 打赏 举报