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

View file

@ -34,6 +34,7 @@ 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.FileOutputStream import java.io.FileOutputStream
import java.io.OutputStream
import java.net.URLDecoder import java.net.URLDecoder
import java.util.* import java.util.*
@ -175,24 +176,40 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private fun saveImageAs() { private fun saveImageAs() {
val currPath = getCurrentMedium()!!.path val currPath = getCurrentMedium()!!.path
SaveAsDialog(this, currPath) { SaveAsDialog(this, currPath) {
var fOut: FileOutputStream? = null var out: OutputStream? = null
try { try {
val file = File(it)
if (file.exists()) {
toast(R.string.file_exists)
return@SaveAsDialog
}
var bitmap = BitmapFactory.decodeFile(currPath) 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() val matrix = Matrix()
matrix.postRotate(mRotationDegrees) matrix.postRotate(mRotationDegrees)
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true) bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
bitmap.compress(file.getCompressionFormat(), 90, out)
val file = File(it) out?.flush()
fOut = FileOutputStream(file)
bitmap.compress(file.getCompressionFormat(), 90, fOut)
fOut.flush()
toast(R.string.file_saved) toast(R.string.file_saved)
} catch (e: OutOfMemoryError) { } catch (e: OutOfMemoryError) {
toast(R.string.out_of_memory_error) toast(R.string.out_of_memory_error)
} catch (e: Exception) { } catch (e: Exception) {
toast(R.string.unknown_error_occurred) toast(R.string.unknown_error_occurred)
} finally { } finally {
fOut?.close() out?.close()
} }
} }
} }