rotate images on the internal storage more efficiently, by modifying exif

This commit is contained in:
tibbi 2018-03-15 18:14:47 +01:00
parent 5e9a4589ec
commit ac08320bc4

View file

@ -522,9 +522,20 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} }
private fun saveImageToFile(oldPath: String, newPath: String) { private fun saveImageToFile(oldPath: String, newPath: String) {
val newFile = File(newPath)
toast(R.string.saving) 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()}") val tmpFile = File(filesDir, ".tmp_${newPath.getFilenameFromPath()}")
try { try {
val bitmap = BitmapFactory.decodeFile(oldPath) val bitmap = BitmapFactory.decodeFile(oldPath)
getFileOutputStream(tmpFile.toFileDirItem(applicationContext)) { getFileOutputStream(tmpFile.toFileDirItem(applicationContext)) {
@ -535,7 +546,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
val oldLastModified = getCurrentFile().lastModified() val oldLastModified = getCurrentFile().lastModified()
if (oldPath.isJpg()) { if (oldPath.isJpg()) {
saveRotation(getCurrentFile(), tmpFile) copyFile(getCurrentFile(), tmpFile)
saveRotation(tmpFile.absolutePath)
} else { } else {
saveFile(tmpFile, bitmap, it as FileOutputStream) saveFile(tmpFile, bitmap, it as FileOutputStream)
} }
@ -594,9 +606,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
bmp.compress(file.absolutePath.getCompressionFormat(), 90, out) bmp.compress(file.absolutePath.getCompressionFormat(), 90, out)
} }
private fun saveRotation(source: File, destination: File) { private fun saveRotation(destinationPath: String) {
copyFile(source, destination) val exif = ExifInterface(destinationPath)
val exif = ExifInterface(destination.absolutePath)
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))