properly save SVG files, not just as fake PNGs

This commit is contained in:
tibbi 2020-09-19 17:38:53 +02:00
parent b83f054bcd
commit 73bb9a18cd
2 changed files with 18 additions and 11 deletions

View File

@ -190,7 +190,11 @@ class MainActivity : SimpleActivity(), CanvasListener {
tryOpenUri(resultData.data!!, resultData)
} else if (requestCode == SAVE_IMAGE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
val outputStream = contentResolver.openOutputStream(resultData.data!!)
saveToOutputStream(outputStream, defaultPath.getCompressionFormat(), false)
if (defaultExtension == SVG) {
Svg.saveToOutputStream(this, outputStream, my_canvas)
} else {
saveToOutputStream(outputStream, defaultPath.getCompressionFormat(), false)
}
savedPathsHash = my_canvas.getDrawingHashCode()
}
}

View File

@ -18,17 +18,20 @@ import java.util.*
object Svg {
fun saveSvg(activity: SimpleActivity, path: String, canvas: MyCanvas) {
val backgroundColor = (canvas.background as ColorDrawable).color
activity.getFileOutputStream(FileDirItem(path, path.getFilenameFromPath()), true) {
if (it != null) {
val writer = BufferedWriter(OutputStreamWriter(it))
writeSvg(writer, backgroundColor, canvas.mPaths, canvas.width, canvas.height)
writer.close()
activity.toast(R.string.file_saved)
} else {
activity.toast(R.string.unknown_error_occurred)
}
saveToOutputStream(activity, it, canvas)
}
}
fun saveToOutputStream(activity: SimpleActivity, outputStream: OutputStream?, canvas: MyCanvas) {
if (outputStream != null) {
val backgroundColor = (canvas.background as ColorDrawable).color
val writer = BufferedWriter(OutputStreamWriter(outputStream))
writeSvg(writer, backgroundColor, canvas.mPaths, canvas.width, canvas.height)
writer.close()
activity.toast(R.string.file_saved)
} else {
activity.toast(R.string.unknown_error_occurred)
}
}