@@ -31,7 +31,6 @@ import java.io.ByteArrayOutputStream
3131import java.io.File
3232import java.io.FileOutputStream
3333
34-
3534class MainActivity : SimpleActivity (), MyCanvas.PathsChangedListener {
3635 private val FOLDER_NAME = " images"
3736 private val FILE_NAME = " simple-draw.png"
@@ -126,10 +125,21 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
126125 handlePermission(PERMISSION_WRITE_STORAGE ) {
127126 if (it) {
128127 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)
128+ tryOpenUri(uri)
129+ } else {
130+ toast(R .string.no_storage_permissions)
131+ }
132+ }
133+ }
134+
135+ if (intent?.action == Intent .ACTION_SEND_MULTIPLE && intent.type.startsWith(" image/" )) {
136+ handlePermission(PERMISSION_WRITE_STORAGE ) {
137+ if (it) {
138+ val imageUris = intent.getParcelableArrayListExtra<Uri >(Intent .EXTRA_STREAM )
139+ for (uri in imageUris) {
140+ if (tryOpenUri(uri)) {
141+ break
142+ }
133143 }
134144 } else {
135145 toast(R .string.no_storage_permissions)
@@ -149,30 +159,49 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
149159 }
150160 }
151161
152- private fun openPath (path : String ) {
153- when {
154- path.endsWith(" .svg" ) -> {
155- my_canvas.mBackgroundBitmap = null
156- Svg .loadSvg(this , File (path), my_canvas)
157- suggestedFileExtension = SVG
158- }
159- File (path).isImageSlow() -> {
160- my_canvas.drawBitmap(this , path)
161- suggestedFileExtension = JPG
162- }
163- else -> toast(R .string.invalid_file_format)
162+ private fun tryOpenUri (uri : Uri ) = when {
163+ uri.scheme == " file" -> openPath(uri.path)
164+ uri.scheme == " content" -> openUri(uri, intent)
165+ else -> false
166+ }
167+
168+ private fun openPath (path : String ) = when {
169+ path.endsWith(" .svg" ) -> {
170+ my_canvas.mBackgroundBitmap = null
171+ Svg .loadSvg(this , File (path), my_canvas)
172+ suggestedFileExtension = SVG
173+ true
174+ }
175+ File (path).isImageSlow() -> {
176+ my_canvas.drawBitmap(this , path)
177+ suggestedFileExtension = JPG
178+ true
179+ }
180+ else -> {
181+ toast(R .string.invalid_file_format)
182+ false
164183 }
165184 }
166185
167- private fun openUri (uri : Uri ) {
186+ private fun openUri (uri : Uri , intent : Intent ): Boolean {
168187 val mime = MimeTypeMap .getSingleton()
169- val type = mime.getExtensionFromMimeType(contentResolver.getType(uri))
170- when (type) {
188+ val type = mime.getExtensionFromMimeType(contentResolver.getType(uri)) ? : intent.type
189+ return when (type) {
190+ " svg" , " image/svg+xml" -> {
191+ my_canvas.mBackgroundBitmap = null
192+ Svg .loadSvg(this , uri, my_canvas)
193+ suggestedFileExtension = SVG
194+ true
195+ }
171196 " jpg" , " jpeg" , " png" -> {
172197 my_canvas.drawBitmap(this , uri)
173198 suggestedFileExtension = JPG
199+ true
200+ }
201+ else -> {
202+ toast(R .string.invalid_file_format)
203+ false
174204 }
175- else -> toast(R .string.invalid_file_format)
176205 }
177206 }
178207
0 commit comments