handle saving on SD card after rotating

This commit is contained in:
tibbi 2017-02-13 23:57:14 +01:00
parent d4bd291461
commit 60cc462c6e
2 changed files with 25 additions and 13 deletions

View file

@ -22,7 +22,6 @@ import com.theartofdev.edmodo.cropper.CropImageView
import kotlinx.android.synthetic.main.activity_edit.*
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream
class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener {
@ -152,11 +151,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
} catch (e: OutOfMemoryError) {
toast(R.string.out_of_memory_error)
} finally {
try {
out?.close()
} catch (e: IOException) {
Log.e(TAG, "FileOutputStream closing failed $e")
}
out?.close()
}
scanPath(path) {

View file

@ -34,6 +34,7 @@ import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.activity_medium.*
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
import java.net.URLDecoder
import java.util.*
@ -175,24 +176,40 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun saveImageAs() {
val currPath = getCurrentMedium()!!.path
SaveAsDialog(this, currPath) {
var fOut: FileOutputStream? = null
var out: OutputStream? = null
try {
val file = File(it)
if (file.exists()) {
toast(R.string.file_exists)
return@SaveAsDialog
}
var bitmap = BitmapFactory.decodeFile(currPath)
if (needsStupidWritePermissions(it)) {
if (isShowingPermDialog(file))
return@SaveAsDialog
var document = getFileDocument(it, config.treeUri)
if (!file.exists()) {
document = document.createFile("", file.name)
}
out = contentResolver.openOutputStream(document.uri)
} else {
out = FileOutputStream(file)
}
val matrix = Matrix()
matrix.postRotate(mRotationDegrees)
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
val file = File(it)
fOut = FileOutputStream(file)
bitmap.compress(file.getCompressionFormat(), 90, fOut)
fOut.flush()
bitmap.compress(file.getCompressionFormat(), 90, out)
out?.flush()
toast(R.string.file_saved)
} catch (e: OutOfMemoryError) {
toast(R.string.out_of_memory_error)
} catch (e: Exception) {
toast(R.string.unknown_error_occurred)
} finally {
fOut?.close()
out?.close()
}
}
}