diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt index 16432c213..a2c2f3379 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -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) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index 676acef0b..2b8638339 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -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() } } }