properly handle saving on sd card
This commit is contained in:
parent
767dd6070a
commit
f4e30de755
4 changed files with 21 additions and 21 deletions
|
@ -32,7 +32,7 @@ android {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile 'com.simplemobiletools:commons:2.16.2'
|
compile 'com.simplemobiletools:commons:2.16.3'
|
||||||
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,18 +4,23 @@ import android.graphics.Color
|
||||||
import android.graphics.drawable.ColorDrawable
|
import android.graphics.drawable.ColorDrawable
|
||||||
import android.sax.RootElement
|
import android.sax.RootElement
|
||||||
import android.util.Xml
|
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.MainActivity
|
||||||
|
import com.simplemobiletools.draw.activities.SimpleActivity
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
object Svg {
|
object Svg {
|
||||||
fun saveSvg(file: File, canvas: MyCanvas) {
|
fun saveSvg(activity: SimpleActivity, file: File, canvas: MyCanvas) {
|
||||||
val backgroundColor = (canvas.background as ColorDrawable).color
|
val backgroundColor = (canvas.background as ColorDrawable).color
|
||||||
|
|
||||||
val out = FileOutputStream(file)
|
activity.getFileOutputStream(file) {
|
||||||
val writer = BufferedWriter(OutputStreamWriter(out))
|
val writer = BufferedWriter(OutputStreamWriter(it))
|
||||||
writeSvg(writer, backgroundColor, canvas.mPaths, canvas.width, canvas.height)
|
writeSvg(writer, backgroundColor, canvas.mPaths, canvas.width, canvas.height)
|
||||||
writer.close()
|
writer.close()
|
||||||
|
activity.toast(R.string.file_saved)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun writeSvg(writer: Writer, backgroundColor: Int, paths: Map<MyPath, PaintOptions>, width: Int, height: Int) {
|
private fun writeSvg(writer: Writer, backgroundColor: Int, paths: Map<MyPath, PaintOptions>, width: Int, height: Int) {
|
||||||
|
|
|
@ -8,10 +8,8 @@ import com.simplemobiletools.draw.MyCanvas
|
||||||
import com.simplemobiletools.draw.R
|
import com.simplemobiletools.draw.R
|
||||||
import com.simplemobiletools.draw.Svg
|
import com.simplemobiletools.draw.Svg
|
||||||
import com.simplemobiletools.draw.activities.SimpleActivity
|
import com.simplemobiletools.draw.activities.SimpleActivity
|
||||||
import com.simplemobiletools.draw.extensions.config
|
|
||||||
import kotlinx.android.synthetic.main.dialog_save_image.view.*
|
import kotlinx.android.synthetic.main.dialog_save_image.view.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
|
|
||||||
class SaveImageDialog(val activity: SimpleActivity, val curPath: String, val canvas: MyCanvas, callback: (path: String) -> Unit) {
|
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)) {
|
if (saveFile(newFile)) {
|
||||||
activity.toast(R.string.file_saved)
|
|
||||||
callback(newFile.absolutePath)
|
callback(newFile.absolutePath)
|
||||||
dismiss()
|
dismiss()
|
||||||
} else {
|
} else {
|
||||||
|
@ -80,7 +77,7 @@ class SaveImageDialog(val activity: SimpleActivity, val curPath: String, val can
|
||||||
}
|
}
|
||||||
|
|
||||||
when (file.extension) {
|
when (file.extension) {
|
||||||
SVG -> Svg.saveSvg(file, canvas)
|
SVG -> Svg.saveSvg(activity, file, canvas)
|
||||||
else -> saveImageFile(file)
|
else -> saveImageFile(file)
|
||||||
}
|
}
|
||||||
activity.scanFile(file) {}
|
activity.scanFile(file) {}
|
||||||
|
@ -88,17 +85,9 @@ class SaveImageDialog(val activity: SimpleActivity, val curPath: String, val can
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveImageFile(file: File) {
|
private fun saveImageFile(file: File) {
|
||||||
if (activity.needsStupidWritePermissions(file.absolutePath)) {
|
activity.getFileOutputStream(file) {
|
||||||
activity.handleSAFDialog(file) {
|
writeToOutputStream(file, it)
|
||||||
var document = activity.getFileDocument(file.absolutePath, activity.config.treeUri) ?: return@handleSAFDialog
|
activity.toast(R.string.file_saved)
|
||||||
if (!file.exists()) {
|
|
||||||
document = document.createFile("", file.name)
|
|
||||||
}
|
|
||||||
val out = activity.contentResolver.openOutputStream(document.uri)
|
|
||||||
writeToOutputStream(file, out)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
writeToOutputStream(file, FileOutputStream(file))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,18 +50,24 @@
|
||||||
android:id="@+id/save_image_radio_png"
|
android:id="@+id/save_image_radio_png"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingBottom="@dimen/medium_margin"
|
||||||
|
android:paddingTop="@dimen/medium_margin"
|
||||||
android:text=".png"/>
|
android:text=".png"/>
|
||||||
|
|
||||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||||
android:id="@+id/save_image_radio_svg"
|
android:id="@+id/save_image_radio_svg"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingBottom="@dimen/medium_margin"
|
||||||
|
android:paddingTop="@dimen/medium_margin"
|
||||||
android:text=".svg"/>
|
android:text=".svg"/>
|
||||||
|
|
||||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||||
android:id="@+id/save_image_radio_jpg"
|
android:id="@+id/save_image_radio_jpg"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingBottom="@dimen/medium_margin"
|
||||||
|
android:paddingTop="@dimen/medium_margin"
|
||||||
android:text=".jpg"/>
|
android:text=".jpg"/>
|
||||||
|
|
||||||
</RadioGroup>
|
</RadioGroup>
|
||||||
|
|
Loading…
Reference in a new issue