Android联系人书籍会覆盖EXIF图像数据

时间:2016-06-07 04:16:28

标签: android exif

在我的应用中,当我将其添加到新的或现有的联系人时,我尝试将EXIF属性添加到联系人照片。这样我以后可以检查是否是My_App更改了照片。我像这样添加EXIF数据:

private void addPhotoToExistingContact(long rawContactId, byte[] photoByteArray) {
    if (photoByteArray != null) {
        try {
            photoByteArray = addExifDataToContactPhoto(photoByteArray);
        } catch (IOException e) {
            e.printStackTrace();
        }

        ContentValues values = new ContentValues();
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, photoByteArray);
        context.getContentResolver().insert(
                ContactsContract.Data.CONTENT_URI,
                values);
    }
}

private byte[] addExifDataToContactPhoto(byte[] photoByteArray) throws IOException {
    // Convert to temp file
    File file = new File(context.getCacheDir(), "contact_photo_exif_temp_file.jpg");

    if (file.exists()) {
        file.delete();
    }

    FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile());
    fos.write(photoByteArray);
    fos.close();

    // Add EXIF data
    ExifInterface exif = new ExifInterface(file.getAbsolutePath());
    exif.setAttribute(MY_EXIF_TAG, MY_EXIF_VALUE);
    exif.saveAttributes();

    // Convert back to byte[]
    byte[] photoByteArrayWithExifData = FileUtils.readFileToByteArray(file);

    // Delete temp file
    file.delete();

    return photoByteArrayWithExifData;
}

我对EXIF数据的检查(稍后完成)如下:

private boolean shouldReplaceContactPhoto(long contactId) {
    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = contentResolver.query(
            ContactsContract.Data.CONTENT_URI,
            null,
            ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?",
            new String[] { ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE, String.valueOf(contactId) },
            null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            long photoId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data.PHOTO_ID));

            if (photoId == 0) {
                cursor.close();
                return true;
            }
            else {
                // Read EXIF data to check if photo is a My_App photo

                File contactPhotoTempFile = getExistingContactImageFile(contactId);

                if (contactPhotoTempFile != null) {
                    try {
                        ExifInterface exif = new ExifInterface(contactPhotoTempFile.getAbsolutePath());
                        String swopTag = exif.getAttribute(MY_EXIF_TAG);

                        // Temporary image, so delete it when we're done reading EXIF data
                        contactPhotoTempFile.delete();

                        cursor.close();

                        // If tag is null, the photo came from a different source - return 'true'
                        // so it is not replaced.
                        return myTag != null;

                    } catch (IOException e) {
                        e.printStackTrace();
                        // Temporary image, so delete it when we're done reading EXIF data
                        contactPhotoTempFile.delete();
                    }
                }
                cursor.close();
                return true;
            }
        }
        cursor.close();
    }

    return true;
}

private File getExistingContactImageFile(long contactId) {
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);

    byte[] imageBytes;

    try {
        AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
        InputStream inputStream = fd.createInputStream();
        imageBytes = IOUtils.toByteArray(inputStream);

    } catch (IOException e) {
        return null;
    }

    if (imageBytes == null) {
        return null;
    }

    File file = new File(context.getCacheDir(), "contact_photo_temp_file.jpg");

    if (file.exists()) {
        file.delete();
    }

    try {
        FileOutputStream fos = new FileOutputStream(file.getPath());
        fos.write(imageBytes);
        fos.close();

    } catch (java.io.IOException e) {
        e.printStackTrace();
    }

    return file;
}

我已经添加了断点和日志语句,并且我几乎100%肯定该属性正在写入,但是当我读取数据时,属性丢失了。我还注意到,orientation属性也从1更改为0,这让我相信Android会覆盖EXIF数据。

是这种情况,还是我做错了什么?任何帮助都非常感谢!

0 个答案:

没有答案
相关问题