From f4e30de7558bfeff321f01b8d2bae26c32f74049 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 9 Apr 2017 16:43:42 +0200 Subject: [PATCH] properly handle saving on sd card --- app/build.gradle | 2 +- .../kotlin/com/simplemobiletools/draw/Svg.kt | 15 ++++++++++----- .../draw/dialogs/SaveImageDialog.kt | 19 ++++--------------- app/src/main/res/layout/dialog_save_image.xml | 6 ++++++ 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a4134bf..360a7d3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,7 +32,7 @@ android { } dependencies { - compile 'com.simplemobiletools:commons:2.16.2' + compile 'com.simplemobiletools:commons:2.16.3' 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 a0d12ff..ce8dcec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt @@ -4,18 +4,23 @@ import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.sax.RootElement import android.util.Xml +import com.simplemobiletools.commons.extensions.getFileOutputStream +import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.draw.activities.MainActivity +import com.simplemobiletools.draw.activities.SimpleActivity import java.io.* import java.util.* object Svg { - fun saveSvg(file: File, canvas: MyCanvas) { + fun saveSvg(activity: SimpleActivity, file: File, canvas: MyCanvas) { val backgroundColor = (canvas.background as ColorDrawable).color - val out = FileOutputStream(file) - val writer = BufferedWriter(OutputStreamWriter(out)) - writeSvg(writer, backgroundColor, canvas.mPaths, canvas.width, canvas.height) - writer.close() + activity.getFileOutputStream(file) { + val writer = BufferedWriter(OutputStreamWriter(it)) + writeSvg(writer, backgroundColor, canvas.mPaths, canvas.width, canvas.height) + writer.close() + activity.toast(R.string.file_saved) + } } private fun writeSvg(writer: Writer, backgroundColor: Int, paths: Map, width: Int, height: Int) { 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 b742654..0457309 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/dialogs/SaveImageDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/dialogs/SaveImageDialog.kt @@ -8,10 +8,8 @@ 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) { @@ -62,7 +60,6 @@ class SaveImageDialog(val activity: SimpleActivity, val curPath: String, val can } if (saveFile(newFile)) { - activity.toast(R.string.file_saved) callback(newFile.absolutePath) dismiss() } else { @@ -80,7 +77,7 @@ class SaveImageDialog(val activity: SimpleActivity, val curPath: String, val can } when (file.extension) { - SVG -> Svg.saveSvg(file, canvas) + SVG -> Svg.saveSvg(activity, file, canvas) else -> saveImageFile(file) } activity.scanFile(file) {} @@ -88,17 +85,9 @@ class SaveImageDialog(val activity: SimpleActivity, val curPath: String, val can } 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)) + activity.getFileOutputStream(file) { + writeToOutputStream(file, it) + activity.toast(R.string.file_saved) } } diff --git a/app/src/main/res/layout/dialog_save_image.xml b/app/src/main/res/layout/dialog_save_image.xml index 281c4b6..ebad450 100644 --- a/app/src/main/res/layout/dialog_save_image.xml +++ b/app/src/main/res/layout/dialog_save_image.xml @@ -50,18 +50,24 @@ android:id="@+id/save_image_radio_png" android:layout_width="match_parent" android:layout_height="wrap_content" + android:paddingBottom="@dimen/medium_margin" + android:paddingTop="@dimen/medium_margin" android:text=".png"/>