move the image saving things in a separate function to reduce indentation

This commit is contained in:
tibbi 2017-11-09 14:11:52 +01:00
parent 21fbde7dad
commit aa5b5b3081
1 changed files with 53 additions and 47 deletions

View File

@ -498,59 +498,65 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
Thread({
val selectedFile = File(it)
handleSAFDialog(selectedFile) {
toast(R.string.saving)
val tmpFile = File(filesDir, ".tmp_${it.getFilenameFromPath()}")
try {
val bitmap = BitmapFactory.decodeFile(currPath)
getFileOutputStream(tmpFile) {
if (it == null) {
toast(R.string.unknown_error_occurred)
return@getFileOutputStream
}
saveImageToFile(currPath, it)
val oldLastModified = getCurrentFile().lastModified()
if (currPath.isJpg()) {
saveRotation(getCurrentFile(), tmpFile)
} else {
saveFile(tmpFile, bitmap, it as FileOutputStream)
}
if (tmpFile.length() > 0 && selectedFile.exists()) {
deleteFile(selectedFile) {}
}
copyFile(tmpFile, selectedFile)
scanFile(selectedFile) {}
toast(R.string.file_saved)
if (config.keepLastModified) {
selectedFile.setLastModified(oldLastModified)
updateLastModified(selectedFile, oldLastModified)
}
it.flush()
it.close()
mRotationDegrees = 0f
invalidateOptionsMenu()
// we cannot refresh a specific image in Glide Cache, so just clear it all
val glide = Glide.get(applicationContext)
glide.clearDiskCache()
runOnUiThread {
glide.clearMemory()
}
}
} catch (e: OutOfMemoryError) {
toast(R.string.out_of_memory_error)
} catch (e: Exception) {
showErrorToast(e)
} finally {
deleteFile(tmpFile) {}
}
}
}).start()
}
}
private fun saveImageToFile(oldPath: String, newPath: String) {
val newFile = File(newPath)
toast(R.string.saving)
val tmpFile = File(filesDir, ".tmp_${newPath.getFilenameFromPath()}")
try {
val bitmap = BitmapFactory.decodeFile(oldPath)
getFileOutputStream(tmpFile) {
if (it == null) {
toast(R.string.unknown_error_occurred)
return@getFileOutputStream
}
val oldLastModified = getCurrentFile().lastModified()
if (oldPath.isJpg()) {
saveRotation(getCurrentFile(), tmpFile)
} else {
saveFile(tmpFile, bitmap, it as FileOutputStream)
}
if (tmpFile.length() > 0 && newFile.exists()) {
deleteFile(newFile) {}
}
copyFile(tmpFile, newFile)
scanFile(newFile) {}
toast(R.string.file_saved)
if (config.keepLastModified) {
newFile.setLastModified(oldLastModified)
updateLastModified(newFile, oldLastModified)
}
it.flush()
it.close()
mRotationDegrees = 0f
invalidateOptionsMenu()
// we cannot refresh a specific image in Glide Cache, so just clear it all
val glide = Glide.get(applicationContext)
glide.clearDiskCache()
runOnUiThread {
glide.clearMemory()
}
}
} catch (e: OutOfMemoryError) {
toast(R.string.out_of_memory_error)
} catch (e: Exception) {
showErrorToast(e)
} finally {
deleteFile(tmpFile) {}
}
}
private fun copyFile(source: File, destination: File) {
var inputStream: InputStream? = null
var out: OutputStream? = null