diff --git a/app/build.gradle b/app/build.gradle index fd443cf..a4134bf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,7 +32,7 @@ android { } dependencies { - compile 'com.simplemobiletools:commons:2.16.1' + compile 'com.simplemobiletools:commons:2.16.2' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt b/app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt index 873d614..a0d12ff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt @@ -9,10 +9,10 @@ import java.io.* import java.util.* object Svg { - fun saveSvg(output: File, canvas: MyCanvas) { + fun saveSvg(file: File, canvas: MyCanvas) { val backgroundColor = (canvas.background as ColorDrawable).color - val out = FileOutputStream(output) + val out = FileOutputStream(file) val writer = BufferedWriter(OutputStreamWriter(out)) writeSvg(writer, backgroundColor, canvas.mPaths, canvas.width, canvas.height) writer.close() diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/dialogs/SaveImageDialog.kt b/app/src/main/kotlin/com/simplemobiletools/draw/dialogs/SaveImageDialog.kt index ca280a8..b742654 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/dialogs/SaveImageDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/dialogs/SaveImageDialog.kt @@ -1,6 +1,5 @@ package com.simplemobiletools.draw.dialogs -import android.graphics.Bitmap import android.support.v7.app.AlertDialog import android.view.WindowManager import com.simplemobiletools.commons.dialogs.FilePickerDialog @@ -9,9 +8,11 @@ import com.simplemobiletools.draw.MyCanvas import com.simplemobiletools.draw.R import com.simplemobiletools.draw.Svg import com.simplemobiletools.draw.activities.SimpleActivity +import com.simplemobiletools.draw.extensions.config import kotlinx.android.synthetic.main.dialog_save_image.view.* import java.io.File import java.io.FileOutputStream +import java.io.OutputStream class SaveImageDialog(val activity: SimpleActivity, val curPath: String, val canvas: MyCanvas, callback: (path: String) -> Unit) { private val PNG = "png" @@ -79,24 +80,34 @@ class SaveImageDialog(val activity: SimpleActivity, val curPath: String, val can } when (file.extension) { - PNG -> { - var out: FileOutputStream? = null - try { - out = FileOutputStream(file) - canvas.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out) - } finally { - out?.close() - } - } - JPG -> { - - } SVG -> Svg.saveSvg(file, canvas) + else -> saveImageFile(file) } activity.scanFile(file) {} return true } + private fun saveImageFile(file: File) { + if (activity.needsStupidWritePermissions(file.absolutePath)) { + activity.handleSAFDialog(file) { + var document = activity.getFileDocument(file.absolutePath, activity.config.treeUri) ?: return@handleSAFDialog + if (!file.exists()) { + document = document.createFile("", file.name) + } + val out = activity.contentResolver.openOutputStream(document.uri) + writeToOutputStream(file, out) + } + } else { + writeToOutputStream(file, FileOutputStream(file)) + } + } + + private fun writeToOutputStream(file: File, out: OutputStream) { + out.use { out -> + canvas.getBitmap().compress(file.getCompressionFormat(), 70, out) + } + } + private fun getInitialFilename(): String { val defaultFilename = "image_${System.currentTimeMillis() / 1000}" return if (curPath.isEmpty()) defaultFilename else curPath.getFilenameFromPath().substring(0, curPath.getFilenameFromPath().lastIndexOf(".")) diff --git a/app/src/main/res/layout/dialog_save_image.xml b/app/src/main/res/layout/dialog_save_image.xml index 98ebfc5..281c4b6 100644 --- a/app/src/main/res/layout/dialog_save_image.xml +++ b/app/src/main/res/layout/dialog_save_image.xml @@ -5,7 +5,9 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" - android:padding="@dimen/activity_margin"> + android:paddingLeft="@dimen/activity_margin" + android:paddingRight="@dimen/activity_margin" + android:paddingTop="@dimen/activity_margin">