handle saving on sd card properly too

This commit is contained in:
tibbi 2017-04-09 16:15:11 +02:00
parent ecdbeab598
commit 767dd6070a
4 changed files with 30 additions and 17 deletions

View File

@ -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"
}

View File

@ -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()

View File

@ -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("."))

View File

@ -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">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/save_image_path_label"