From 5a4a35f6d2f3840e6d60b7a14731288681eff85f Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 15 Mar 2018 18:55:16 +0100 Subject: [PATCH] rotate SD card files by exif when possible --- .../gallery/activities/ViewPagerActivity.kt | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index db76eaa31..69103665f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.gallery.activities import android.animation.Animator import android.animation.ValueAnimator +import android.annotation.TargetApi import android.app.Activity import android.content.Intent import android.content.pm.ActivityInfo @@ -521,15 +522,32 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } } + @TargetApi(Build.VERSION_CODES.N) private fun saveImageToFile(oldPath: String, newPath: String) { toast(R.string.saving) - if (oldPath == newPath && oldPath.isJpg() && !isPathOnSD(oldPath)) { + if (oldPath == newPath && oldPath.isJpg()) { try { - saveRotation(oldPath) - toast(R.string.file_saved) - return + if (!isPathOnSD(oldPath)) { + saveExifRotation(ExifInterface(oldPath)) + mRotationDegrees = 0f + invalidateOptionsMenu() + toast(R.string.file_saved) + return + } else if (isNougatPlus()) { + val documentFile = getSomeDocumentFile(oldPath) + if (documentFile != null) { + val parcelFileDescriptor = contentResolver.openFileDescriptor(documentFile.uri, "rw") + val fileDescriptor = parcelFileDescriptor.fileDescriptor + saveExifRotation(ExifInterface(fileDescriptor)) + mRotationDegrees = 0f + invalidateOptionsMenu() + toast(R.string.file_saved) + return + } + } } catch (e: Exception) { showErrorToast(e) + return } } @@ -547,7 +565,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View val oldLastModified = getCurrentFile().lastModified() if (oldPath.isJpg()) { copyFile(getCurrentFile(), tmpFile) - saveRotation(tmpFile.absolutePath) + saveExifRotation(ExifInterface(tmpFile.absolutePath)) } else { saveFile(tmpFile, bitmap, it as FileOutputStream) } @@ -606,8 +624,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View bmp.compress(file.absolutePath.getCompressionFormat(), 90, out) } - private fun saveRotation(destinationPath: String) { - val exif = ExifInterface(destinationPath) + private fun saveExifRotation(exif: ExifInterface) { val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) val orientationDegrees = (degreesForRotation(orientation) + mRotationDegrees) % 360 exif.setAttribute(ExifInterface.TAG_ORIENTATION, rotationFromDegrees(orientationDegrees))