rotate SD card files by exif when possible

This commit is contained in:
tibbi 2018-03-15 18:55:16 +01:00
parent ac08320bc4
commit 5a4a35f6d2

View file

@ -2,6 +2,7 @@ package com.simplemobiletools.gallery.activities
import android.animation.Animator import android.animation.Animator
import android.animation.ValueAnimator import android.animation.ValueAnimator
import android.annotation.TargetApi
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.content.pm.ActivityInfo 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) { private fun saveImageToFile(oldPath: String, newPath: String) {
toast(R.string.saving) toast(R.string.saving)
if (oldPath == newPath && oldPath.isJpg() && !isPathOnSD(oldPath)) { if (oldPath == newPath && oldPath.isJpg()) {
try { try {
saveRotation(oldPath) if (!isPathOnSD(oldPath)) {
saveExifRotation(ExifInterface(oldPath))
mRotationDegrees = 0f
invalidateOptionsMenu()
toast(R.string.file_saved) toast(R.string.file_saved)
return 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) { } catch (e: Exception) {
showErrorToast(e) showErrorToast(e)
return
} }
} }
@ -547,7 +565,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
val oldLastModified = getCurrentFile().lastModified() val oldLastModified = getCurrentFile().lastModified()
if (oldPath.isJpg()) { if (oldPath.isJpg()) {
copyFile(getCurrentFile(), tmpFile) copyFile(getCurrentFile(), tmpFile)
saveRotation(tmpFile.absolutePath) saveExifRotation(ExifInterface(tmpFile.absolutePath))
} else { } else {
saveFile(tmpFile, bitmap, it as FileOutputStream) saveFile(tmpFile, bitmap, it as FileOutputStream)
} }
@ -606,8 +624,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
bmp.compress(file.absolutePath.getCompressionFormat(), 90, out) bmp.compress(file.absolutePath.getCompressionFormat(), 90, out)
} }
private fun saveRotation(destinationPath: String) { private fun saveExifRotation(exif: ExifInterface) {
val exif = ExifInterface(destinationPath)
val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
val orientationDegrees = (degreesForRotation(orientation) + mRotationDegrees) % 360 val orientationDegrees = (degreesForRotation(orientation) + mRotationDegrees) % 360
exif.setAttribute(ExifInterface.TAG_ORIENTATION, rotationFromDegrees(orientationDegrees)) exif.setAttribute(ExifInterface.TAG_ORIENTATION, rotationFromDegrees(orientationDegrees))