0

我不明白为什么它可以在模拟器上运行,但不能在真实设备上运行。

请帮忙!!!

在这里,我通过电话号码获取联系人 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();
    }

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

4

1 回答 1

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());
于 2013-10-23T10:08:41.153 回答