updating the way photos are rotated and saved
This commit is contained in:
parent
0cc2c3d8f1
commit
b72686cefd
2 changed files with 66 additions and 45 deletions
|
@ -46,8 +46,8 @@ import com.simplemobiletools.gallery.models.Medium
|
||||||
import kotlinx.android.synthetic.main.activity_medium.*
|
import kotlinx.android.synthetic.main.activity_medium.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.io.FileOutputStream
|
|
||||||
import java.io.FileNotFoundException
|
import java.io.FileNotFoundException
|
||||||
|
import java.io.FileOutputStream
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, ViewPagerFragment.FragmentListener {
|
class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, ViewPagerFragment.FragmentListener {
|
||||||
|
@ -454,48 +454,55 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
|
||||||
SaveAsDialog(this, currPath, false) {
|
SaveAsDialog(this, currPath, false) {
|
||||||
Thread({
|
Thread({
|
||||||
toast(R.string.saving)
|
toast(R.string.saving)
|
||||||
val selectedFile = File(it)
|
if (it.isJpg() && !isPathOnSD(it)) {
|
||||||
val tmpFile = File(selectedFile.parent, ".tmp_${it.getFilenameFromPath()}")
|
if (it == currPath) {
|
||||||
try {
|
rotateFileByExif(it)
|
||||||
val bitmap = BitmapFactory.decodeFile(currPath)
|
runOnUiThread {
|
||||||
getFileOutputStream(tmpFile) {
|
(getCurrentFragment() as? PhotoFragment)?.refreshBitmap()
|
||||||
if (it == null) {
|
|
||||||
toast(R.string.unknown_error_occurred)
|
|
||||||
deleteFile(tmpFile) {}
|
|
||||||
return@getFileOutputStream
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if (currPath.isJpg()) {
|
copyFile(currPath, it)
|
||||||
saveRotation(currPath, tmpFile)
|
rotateFileByExif(it)
|
||||||
} else {
|
|
||||||
saveFile(tmpFile, bitmap, it as FileOutputStream)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (needsStupidWritePermissions(selectedFile.absolutePath)) {
|
|
||||||
deleteFile(selectedFile) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
renameFile(tmpFile, selectedFile) {
|
|
||||||
deleteFile(tmpFile) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
it.flush()
|
|
||||||
it.close()
|
|
||||||
toast(R.string.file_saved)
|
|
||||||
mRotationDegrees = 0f
|
|
||||||
invalidateOptionsMenu()
|
|
||||||
}
|
}
|
||||||
} catch (e: OutOfMemoryError) {
|
} else {
|
||||||
toast(R.string.out_of_memory_error)
|
rotateFileByDegrees(currPath, it)
|
||||||
deleteFile(tmpFile) {}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
showErrorToast(e)
|
|
||||||
deleteFile(tmpFile) {}
|
|
||||||
}
|
}
|
||||||
}).start()
|
}).start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun rotateFileByDegrees(sourcePath: String, destinationPath: String) {
|
||||||
|
val tmpFile = File(File(destinationPath).parent, ".tmp_${destinationPath.getFilenameFromPath()}")
|
||||||
|
try {
|
||||||
|
getFileOutputStream(tmpFile) {
|
||||||
|
if (it == null) {
|
||||||
|
toast(R.string.unknown_error_occurred)
|
||||||
|
return@getFileOutputStream
|
||||||
|
}
|
||||||
|
|
||||||
|
val bitmap = BitmapFactory.decodeFile(sourcePath)
|
||||||
|
saveFile(tmpFile, bitmap, it as FileOutputStream)
|
||||||
|
it.flush()
|
||||||
|
it.close()
|
||||||
|
|
||||||
|
val destination = File(destinationPath)
|
||||||
|
deleteFile(destination) {
|
||||||
|
renameFile(tmpFile, destination) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
toast(R.string.file_saved)
|
||||||
|
mRotationDegrees = 0f
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
}
|
||||||
|
} catch (e: OutOfMemoryError) {
|
||||||
|
toast(R.string.out_of_memory_error)
|
||||||
|
deleteFile(tmpFile) {}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
showErrorToast(e)
|
||||||
|
deleteFile(tmpFile) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun saveFile(file: File, bitmap: Bitmap, out: FileOutputStream) {
|
private fun saveFile(file: File, bitmap: Bitmap, out: FileOutputStream) {
|
||||||
val matrix = Matrix()
|
val matrix = Matrix()
|
||||||
matrix.postRotate(mRotationDegrees)
|
matrix.postRotate(mRotationDegrees)
|
||||||
|
@ -503,25 +510,34 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
|
||||||
bmp.compress(file.getCompressionFormat(), 90, out)
|
bmp.compress(file.getCompressionFormat(), 90, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveRotation(input: String, out: File) {
|
private fun copyFile(sourcePath: String, destinationPath: String) {
|
||||||
var inputStream: FileInputStream? = null
|
var inputStream: FileInputStream? = null
|
||||||
var outputStream: FileOutputStream? = null
|
var outputStream: FileOutputStream? = null
|
||||||
try {
|
try {
|
||||||
inputStream = FileInputStream(input)
|
inputStream = FileInputStream(sourcePath)
|
||||||
outputStream = FileOutputStream(out)
|
outputStream = FileOutputStream(destinationPath)
|
||||||
inputStream.copyTo(outputStream)
|
inputStream.copyTo(outputStream)
|
||||||
|
scanPath(destinationPath) {}
|
||||||
} catch (ignored: FileNotFoundException) {
|
} catch (ignored: FileNotFoundException) {
|
||||||
} finally {
|
} finally {
|
||||||
inputStream?.close()
|
inputStream?.close()
|
||||||
outputStream?.close()
|
outputStream?.close()
|
||||||
}
|
}
|
||||||
if (out.exists()) {
|
}
|
||||||
val exif = ExifInterface(out.absolutePath)
|
|
||||||
var orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
|
private fun rotateFileByExif(path: String) {
|
||||||
var orientationDegrees = (degreesForRotation(orientation) + mRotationDegrees) % 360
|
val exif = ExifInterface(path)
|
||||||
exif.setAttribute(ExifInterface.TAG_ORIENTATION, rotationFromDegrees(orientationDegrees))
|
val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
|
||||||
exif.saveAttributes()
|
val orientationDegrees = (degreesForRotation(orientation) + mRotationDegrees) % 360
|
||||||
|
exif.setAttribute(ExifInterface.TAG_ORIENTATION, rotationFromDegrees(orientationDegrees))
|
||||||
|
exif.saveAttributes()
|
||||||
|
if (!config.keepLastModified) {
|
||||||
|
File(getCurrentPath()).setLastModified(System.currentTimeMillis())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mRotationDegrees = 0f
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
toast(R.string.file_saved)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun degreesForRotation(orientation: Int) = when (orientation) {
|
private fun degreesForRotation(orientation: Int) = when (orientation) {
|
||||||
|
|
|
@ -322,4 +322,9 @@ class PhotoFragment : ViewPagerFragment() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun refreshBitmap() {
|
||||||
|
view.subsampling_view.beGone()
|
||||||
|
loadBitmap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue