From ac08320bc4ce2f16b4a89214bed49724a9075f07 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 15 Mar 2018 18:14:47 +0100 Subject: [PATCH] rotate images on the internal storage more efficiently, by modifying exif --- .../gallery/activities/ViewPagerActivity.kt | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 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 5077999da..db76eaa31 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -522,9 +522,20 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun saveImageToFile(oldPath: String, newPath: String) { - val newFile = File(newPath) toast(R.string.saving) + if (oldPath == newPath && oldPath.isJpg() && !isPathOnSD(oldPath)) { + try { + saveRotation(oldPath) + toast(R.string.file_saved) + return + } catch (e: Exception) { + showErrorToast(e) + } + } + + val newFile = File(newPath) val tmpFile = File(filesDir, ".tmp_${newPath.getFilenameFromPath()}") + try { val bitmap = BitmapFactory.decodeFile(oldPath) getFileOutputStream(tmpFile.toFileDirItem(applicationContext)) { @@ -535,7 +546,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View val oldLastModified = getCurrentFile().lastModified() if (oldPath.isJpg()) { - saveRotation(getCurrentFile(), tmpFile) + copyFile(getCurrentFile(), tmpFile) + saveRotation(tmpFile.absolutePath) } else { saveFile(tmpFile, bitmap, it as FileOutputStream) } @@ -594,9 +606,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View bmp.compress(file.absolutePath.getCompressionFormat(), 90, out) } - private fun saveRotation(source: File, destination: File) { - copyFile(source, destination) - val exif = ExifInterface(destination.absolutePath) + private fun saveRotation(destinationPath: String) { + val exif = ExifInterface(destinationPath) val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) val orientationDegrees = (degreesForRotation(orientation) + mRotationDegrees) % 360 exif.setAttribute(ExifInterface.TAG_ORIENTATION, rotationFromDegrees(orientationDegrees))