@@ -9,6 +9,7 @@ import android.os.Bundle
99import android.support.v4.content.FileProvider
1010import android.view.Menu
1111import android.view.MenuItem
12+ import android.webkit.MimeTypeMap
1213import android.widget.SeekBar
1314import com.simplemobiletools.commons.dialogs.ColorPickerDialog
1415import com.simplemobiletools.commons.dialogs.FilePickerDialog
@@ -30,6 +31,7 @@ import java.io.ByteArrayOutputStream
3031import java.io.File
3132import java.io.FileOutputStream
3233
34+
3335class MainActivity : SimpleActivity (), MyCanvas.PathsChangedListener {
3436 private val FOLDER_NAME = " images"
3537 private val FILE_NAME = " simple-draw.png"
@@ -43,6 +45,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
4345 override fun onCreate (savedInstanceState : Bundle ? ) {
4446 super .onCreate(savedInstanceState)
4547 setContentView(R .layout.activity_main)
48+ storeStoragePaths()
4649 my_canvas.setListener(this )
4750 stroke_width_bar.setOnSeekBarChangeListener(onStrokeWidthBarChangeListener)
4851
@@ -56,18 +59,8 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
5659 color_picker.setOnClickListener { pickColor() }
5760 undo.setOnClickListener { my_canvas.undo() }
5861 eraser.setOnClickListener { eraserClicked() }
59- storeStoragePaths()
6062
61- if (intent?.action == Intent .ACTION_VIEW && intent.data != null ) {
62- val path = intent.data!! .path
63- handlePermission(PERMISSION_WRITE_STORAGE ) {
64- if (it) {
65- openPath(path)
66- } else {
67- toast(R .string.no_storage_permissions)
68- }
69- }
70- }
63+ checkIntents()
7164 checkWhatsNewDialog()
7265 }
7366
@@ -128,6 +121,34 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
128121 }
129122 }
130123
124+ private fun checkIntents () {
125+ if (intent?.action == Intent .ACTION_SEND && intent.type.startsWith(" image/" )) {
126+ handlePermission(PERMISSION_WRITE_STORAGE ) {
127+ if (it) {
128+ val uri = intent.getParcelableExtra<Uri >(Intent .EXTRA_STREAM )
129+ if (uri.scheme == " file" ) {
130+ openPath(uri.path)
131+ } else if (uri.scheme == " content" ) {
132+ openUri(uri)
133+ }
134+ } else {
135+ toast(R .string.no_storage_permissions)
136+ }
137+ }
138+ }
139+
140+ if (intent?.action == Intent .ACTION_VIEW && intent.data != null ) {
141+ val path = intent.data!! .path
142+ handlePermission(PERMISSION_WRITE_STORAGE ) {
143+ if (it) {
144+ openPath(path)
145+ } else {
146+ toast(R .string.no_storage_permissions)
147+ }
148+ }
149+ }
150+ }
151+
131152 private fun openPath (path : String ) {
132153 when {
133154 path.endsWith(" .svg" ) -> {
@@ -143,6 +164,18 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
143164 }
144165 }
145166
167+ private fun openUri (uri : Uri ) {
168+ val mime = MimeTypeMap .getSingleton()
169+ val type = mime.getExtensionFromMimeType(contentResolver.getType(uri))
170+ when (type) {
171+ " jpg" , " jpeg" , " png" -> {
172+ my_canvas.drawBitmap(this , uri)
173+ suggestedFileExtension = JPG
174+ }
175+ else -> toast(R .string.invalid_file_format)
176+ }
177+ }
178+
146179 private fun eraserClicked () {
147180 isEraserOn = ! isEraserOn
148181 updateEraserState()
0 commit comments