try handling a few more share intents

This commit is contained in:
tibbi 2017-10-22 11:30:24 +02:00
parent 294b9df67d
commit b29f7f07cc
3 changed files with 65 additions and 25 deletions

View File

@ -40,6 +40,12 @@
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
</intent-filter>
</activity>
<activity

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.draw
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.sax.RootElement
import android.util.Xml
import com.simplemobiletools.commons.extensions.getFileOutputStream
@ -51,8 +52,8 @@ object Svg {
}
}
fun loadSvg(activity: MainActivity, file: File, canvas: MyCanvas) {
val svg = parseSvg(file)
fun loadSvg(activity: MainActivity, fileOrUri: Any, canvas: MyCanvas) {
val svg = parseSvg(activity, fileOrUri)
canvas.clearCanvas()
activity.setBackgroundColor(svg.background!!.color)
@ -66,11 +67,15 @@ object Svg {
}
}
private fun parseSvg(file: File): SSvg {
private fun parseSvg(activity: MainActivity, fileOrUri: Any): SSvg {
var inputStream: InputStream? = null
val svg = SSvg()
try {
inputStream = FileInputStream(file)
inputStream = when (fileOrUri) {
is File -> FileInputStream(fileOrUri)
is Uri -> activity.contentResolver.openInputStream(fileOrUri)
else -> null
}
// Actual parsing (http://stackoverflow.com/a/4828765)
val ns = "http://www.w3.org/2000/svg"

View File

@ -31,7 +31,6 @@ import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
private val FOLDER_NAME = "images"
private val FILE_NAME = "simple-draw.png"
@ -126,10 +125,21 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
if (uri.scheme == "file") {
openPath(uri.path)
} else if (uri.scheme == "content") {
openUri(uri)
tryOpenUri(uri)
} else {
toast(R.string.no_storage_permissions)
}
}
}
if (intent?.action == Intent.ACTION_SEND_MULTIPLE && intent.type.startsWith("image/")) {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
val imageUris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
for (uri in imageUris) {
if (tryOpenUri(uri)) {
break
}
}
} else {
toast(R.string.no_storage_permissions)
@ -149,30 +159,49 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
}
}
private fun openPath(path: String) {
when {
path.endsWith(".svg") -> {
my_canvas.mBackgroundBitmap = null
Svg.loadSvg(this, File(path), my_canvas)
suggestedFileExtension = SVG
}
File(path).isImageSlow() -> {
my_canvas.drawBitmap(this, path)
suggestedFileExtension = JPG
}
else -> toast(R.string.invalid_file_format)
private fun tryOpenUri(uri: Uri) = when {
uri.scheme == "file" -> openPath(uri.path)
uri.scheme == "content" -> openUri(uri, intent)
else -> false
}
private fun openPath(path: String) = when {
path.endsWith(".svg") -> {
my_canvas.mBackgroundBitmap = null
Svg.loadSvg(this, File(path), my_canvas)
suggestedFileExtension = SVG
true
}
File(path).isImageSlow() -> {
my_canvas.drawBitmap(this, path)
suggestedFileExtension = JPG
true
}
else -> {
toast(R.string.invalid_file_format)
false
}
}
private fun openUri(uri: Uri) {
private fun openUri(uri: Uri, intent: Intent): Boolean {
val mime = MimeTypeMap.getSingleton()
val type = mime.getExtensionFromMimeType(contentResolver.getType(uri))
when (type) {
val type = mime.getExtensionFromMimeType(contentResolver.getType(uri)) ?: intent.type
return when (type) {
"svg", "image/svg+xml" -> {
my_canvas.mBackgroundBitmap = null
Svg.loadSvg(this, uri, my_canvas)
suggestedFileExtension = SVG
true
}
"jpg", "jpeg", "png" -> {
my_canvas.drawBitmap(this, uri)
suggestedFileExtension = JPG
true
}
else -> {
toast(R.string.invalid_file_format)
false
}
else -> toast(R.string.invalid_file_format)
}
}