无法更新或添加新的联系人照片Android

时间:2013-04-08 12:12:12

标签: android contacts android-contentprovider android-contacts android-contentresolver

我无法理解为什么它可以在模拟器上运行但不能在真实设备上运行。

请帮助!!!

我在这里通过电话号码获取联系人ID

    ContentResolver crFull = context.getContentResolver(); 
    Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(PhoneNumberFormatter.GetOnlyNumbers(((Contact)recipients.get(k)).GetContactPhoneNumber())));
    Cursor cursor = crFull.query(lookupUri, new String[] { ContactsContract.PhoneLookup._ID }, null, null, null);
    long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));

我在这里转换Base64的新图片

    String base64Image = json.getString("PhotoBase64");
    byte[] decodedString = Base64.decode(base64Image, Base64.DEFAULT);

    Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    ByteArrayOutputStream streamy = new ByteArrayOutputStream(); 
    bmp.compress(CompressFormat.JPEG, 100, streamy); 
    byte[] photo = streamy.toByteArray();

我在这里检查联系人是否已有图片

    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(crFull, uri);

如果有

    int photoRow = -1;
    String where = ContactsContract.Data.RAW_CONTACT_ID + " = " + id + " AND " + ContactsContract.Data.MIMETYPE + " =='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
    Cursor cursorr = crFull.query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
    int idIdx = cursorr.getColumnIndexOrThrow(ContactsContract.Data._ID);
    if (cursorr.moveToFirst()) {
        photoRow = cursorr.getInt(idIdx);
    }

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
    .withSelection(ContactsContract.Data._ID + " == " + Integer.toString(photoRow), new String[] {})
    .withValue(ContactsContract.Data.RAW_CONTACT_ID, id)
    .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.Data.DATA15, photo)
    .build());

    try {
            crFull.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {

    } catch (OperationApplicationException e) {
        e.printStackTrace();
    }   

如果不是

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
    .withValue(ContactsContract.Data.RAW_CONTACT_ID, id)
    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, photo)
    .build());

    try 
    {
        crFull.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {

    } catch (OperationApplicationException e) {
        e.printStackTrace();
    }

仅在模拟器上更新和插入Works。如果我在真实设备上启动它,则不会更新并插入图像。在真实设备上我也没有例外。 我做错了什么?

1 个答案:

答案 0 :(得分:0)

试试这个...

Bitmap bitmap = ((BitmapDrawable) image.getDrawable())
                .getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
byte[] b = baos.toByteArray();


ops.add(ContentProviderOperation
                            .newUpdate(
                                    ContactsContract.Data.CONTENT_URI)
                            .withSelection(
                                    ContactsContract.Data.CONTACT_ID
                                            + " = ? AND "
                                            + ContactsContract.Data.MIMETYPE
                                            + " = ?",
                                    new String[] {
                                            contactid,
                                            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE })
                            .withValue(Photo.DATA15, b).build());