gpt4 book ai didi

android - 以编程方式保存照片联系人 - Android

转载 作者:行者123 更新时间:2023-11-29 16:45:19 25 4
gpt4 key购买 nike

我实际上是这样做的:

    InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(contactId)));

if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}else{
// To open up a gallery browser
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Seleccione una imagen para el contacto"),1);
}
return photo;

使用该代码,我可以通过电话号码检索联系人照片,当联系人没有照片时,我需要按 Intent 从图库中保存选定的照片:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == RESULT_OK) {

if (requestCode == 1) {
currImageURI = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), currImageURI);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

有人知道如何在联系人中保存此位图吗?谢谢!

最佳答案

使用这段代码:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
if(mBitmap!=null){ // If an image is selected successfully
mBitmap.compress(Bitmap.CompressFormat.PNG , 75, stream);

// Adding insert operation to operations list
// to insert Photo in the table ContactsContract.Data
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
.withValue(ContactsContract.Data.MIMETYPE,Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO,stream.toByteArray())
.build());

try {
stream.flush();
}catch (IOException e) {
e.printStackTrace();
}
}

关于android - 以编程方式保存照片联系人 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48686429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com