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 {
|
||||
compile 'com.simplemobiletools:commons:2.16.2'
|
||||
compile 'com.simplemobiletools:commons:2.16.3'
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
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<MyPath, PaintOptions>, width: Int, height: Int) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||
android:id="@+id/save_image_radio_svg"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingBottom="@dimen/medium_margin"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:text=".svg"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyCompatRadioButton
|
||||
android:id="@+id/save_image_radio_jpg"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingBottom="@dimen/medium_margin"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:text=".jpg"/>
|
||||
|
||||
</RadioGroup>
|
||||
|
|
Loading…
Reference in a new issue