Android:使用 ContactsContract 插入意​​图设置联系人照片

发布于 2024-11-16 20:45:37 字数 864 浏览 7 评论 0原文

我正在使用 ContactsContract 通过将用户发送到“新联系人”意图来插入新联系人。我使用的代码是:

Intent i = new Intent(Intent.ACTION_INSERT);

i.setType(Contacts.CONTENT_TYPE);
i.putExtra(Insert.NAME, "Some Contact Name");
i.putExtra(Insert.EMAIL, "[email protected]");
i.putExtra(Insert.PHONE, "123-456-7890");

startActivity(i);

但是,我还需要以某种方式传递本地存储的照片(在 res/drawable 中)以显示此“新联系人”意图。我希望有一种简单的方法可以做到这一点,

i.putExtra(Insert.PHOTO, uri_to_photo);

但是这显然行不通。我发现此帖子详细介绍了如何设置照片已经存在的联系人(通过 Jak 的 setPhoto() 方法),但没有介绍如何传递照片以在“新联系人”意图上显示为联系人图标。

将照片(希望作为照片的 URI)传递到“新联系人”意图的最佳方式是什么?

提前致谢。

I am using ContactsContract to insert a new contact by sending the user to a "New contact" intent. The code I am using is:

Intent i = new Intent(Intent.ACTION_INSERT);

i.setType(Contacts.CONTENT_TYPE);
i.putExtra(Insert.NAME, "Some Contact Name");
i.putExtra(Insert.EMAIL, "[email protected]");
i.putExtra(Insert.PHONE, "123-456-7890");

startActivity(i);

However, I need to also somehow pass in a locally stored photo (in res/drawable) to show up on this "New contact" intent. I was hoping that there would be an easy way to do this, like

i.putExtra(Insert.PHOTO, uri_to_photo);

but that obviously doesn't work. I found this thread detailing how to set the photo for an already-existing contact (via Jak's setPhoto() method), but nothing on how to pass a photo in to show up as the contact icon on the "New contact" intent.

What would be the best way to pass a photo (hopefully as a URI to the photo) in to the "New contact" intent?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城半夏 2024-11-23 20:45:37

首先使用ContentProviderOperation的方式插入一个新的Contact。

final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                .build());

        Bitmap bmp = YCardImageLoader.getInstance().getBitmapByCache(mTask.getImageUrl());
        if (bmp != null ) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(CompressFormat.JPEG, 100, stream);
            byte[] bytes = stream.toByteArray();

            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                    .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                    .withValue(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE)
                    .withValue(Photo.PHOTO, bytes)
                    .build());
        }

        ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.DISPLAY_NAME, mContact.getName())
                .build());

        ContentProviderResult[] result = SaveToPbkActivity.this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

然后获取结果 uri 作为 ACTION_EDIT uri,添加其他额外内容,startActivityForResult(intent, REQUEST_INSERT_CONTACT)。

        Intent editIntent = new Intent(Intent.ACTION_EDIT);
        uri = result[0].uri;
        editIntent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
        editIntent.putExtra("finishActivityOnSaveCompleted", true);
        putExtras(editIntent, null);
        startActivityForResult(editIntent, REQUEST_INSERT_CONTACT);

因为我们先插入,所以当 resultCode != RESULT_OK 时我们会删除它

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
    if (requestCode == REQUEST_INSERT_CONTACT) {
        if (resultCode == RESULT_OK) {
            //SAVE SUCCESS
        } else {
            ContentResolver cr = getContentResolver();
            cr.delete(uri, null, null);
        }
    } }

最后对不起我的英语!

Firstly use ContentProviderOperation's way to insert a new Contact.

final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                .build());

        Bitmap bmp = YCardImageLoader.getInstance().getBitmapByCache(mTask.getImageUrl());
        if (bmp != null ) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(CompressFormat.JPEG, 100, stream);
            byte[] bytes = stream.toByteArray();

            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                    .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                    .withValue(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE)
                    .withValue(Photo.PHOTO, bytes)
                    .build());
        }

        ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.DISPLAY_NAME, mContact.getName())
                .build());

        ContentProviderResult[] result = SaveToPbkActivity.this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

Then get result uri as the ACTION_EDIT uri, put other extras, startActivityForResult(intent, REQUEST_INSERT_CONTACT).

        Intent editIntent = new Intent(Intent.ACTION_EDIT);
        uri = result[0].uri;
        editIntent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
        editIntent.putExtra("finishActivityOnSaveCompleted", true);
        putExtras(editIntent, null);
        startActivityForResult(editIntent, REQUEST_INSERT_CONTACT);

because we insert first, we will delete it when resultCode != RESULT_OK

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
    if (requestCode == REQUEST_INSERT_CONTACT) {
        if (resultCode == RESULT_OK) {
            //SAVE SUCCESS
        } else {
            ContentResolver cr = getContentResolver();
            cr.delete(uri, null, null);
        }
    } }

At last sorry for my english!

离不开的别离 2024-11-23 20:45:37

尝试使用此代码使用意图添加带有联系方式的图像

 var bit = BitmapFactory.decodeResource(getResources(), R.drawable.yourimagename); // your image
    val data = ArrayList<ContentValues>();
    var row = ContentValues();
    row.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
    val stream = ByteArrayOutputStream()
    bit.compress(Bitmap.CompressFormat.PNG, 100, stream)
    val byteArray = stream.toByteArray()
    bit.recycle()
    row.put(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArray);
    data.add(row)
    var intent = Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
    intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
    intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
    intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone1);
    intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, phone2);
    intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
    context.startActivity(intent);

Try this code to add image with contact details using intent

 var bit = BitmapFactory.decodeResource(getResources(), R.drawable.yourimagename); // your image
    val data = ArrayList<ContentValues>();
    var row = ContentValues();
    row.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
    val stream = ByteArrayOutputStream()
    bit.compress(Bitmap.CompressFormat.PNG, 100, stream)
    val byteArray = stream.toByteArray()
    bit.recycle()
    row.put(ContactsContract.CommonDataKinds.Photo.PHOTO, byteArray);
    data.add(row)
    var intent = Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
    intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
    intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
    intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone1);
    intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, phone2);
    intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
    context.startActivity(intent);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文