diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt index 52777cd1d..5f9aab147 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt @@ -48,10 +48,10 @@ fun Activity.shareMedium(medium: Medium) { fun Activity.shareMedia(media: List) { val shareTitle = resources.getString(R.string.share_via) + val uris = ArrayList(media.size) Intent().apply { action = Intent.ACTION_SEND_MULTIPLE type = "image/* video/*" - val uris = ArrayList(media.size) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) media.map { File(it.path) } .mapTo(uris) { Uri.fromFile(it) } @@ -62,42 +62,48 @@ fun Activity.shareMedia(media: List) { } fun Activity.setAsWallpaper(file: File) { - val intent = Intent(Intent.ACTION_ATTACH_DATA) val uri = Uri.fromFile(file) - intent.setDataAndType(uri, file.getMimeType("image/*")) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - val chooser = Intent.createChooser(intent, getString(R.string.set_as_wallpaper_with)) + Intent().apply { + action = Intent.ACTION_ATTACH_DATA + setDataAndType(uri, file.getMimeType("image/*")) + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + val chooser = Intent.createChooser(this, getString(R.string.set_as_wallpaper_with)) - if (intent.resolveActivity(packageManager) != null) { - startActivityForResult(chooser, REQUEST_SET_WALLPAPER) - } else { - toast(R.string.no_wallpaper_setter_found) + if (resolveActivity(packageManager) != null) { + startActivityForResult(chooser, REQUEST_SET_WALLPAPER) + } else { + toast(R.string.no_wallpaper_setter_found) + } } } fun Activity.openWith(file: File, forceChooser: Boolean = true) { - val intent = Intent(Intent.ACTION_VIEW) val uri = Uri.fromFile(file) - intent.setDataAndType(uri, file.getMimeType("image/jpeg")) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - val chooser = Intent.createChooser(intent, getString(R.string.open_with)) + Intent().apply { + action = Intent.ACTION_VIEW + setDataAndType(uri, file.getMimeType("image/jpeg")) + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + val chooser = Intent.createChooser(this, getString(R.string.open_with)) - if (intent.resolveActivity(packageManager) != null) { - startActivity(if (forceChooser) chooser else intent) - } else { - toast(R.string.no_app_found) + if (resolveActivity(packageManager) != null) { + startActivity(if (forceChooser) chooser else this) + } else { + toast(R.string.no_app_found) + } } } fun Activity.openEditor(file: File) { - val intent = Intent(Intent.ACTION_EDIT) - intent.setDataAndType(Uri.fromFile(file), "image/*") - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + Intent().apply { + action = Intent.ACTION_EDIT + setDataAndType(Uri.fromFile(file), "image/*") + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - if (intent.resolveActivity(packageManager) != null) { - startActivityForResult(intent, REQUEST_EDIT_IMAGE) - } else { - toast(R.string.no_editor_found) + if (resolveActivity(packageManager) != null) { + startActivityForResult(this, REQUEST_EDIT_IMAGE) + } else { + toast(R.string.no_editor_found) + } } }