Share Text, Images, Multiple Images Using Kotlin
Intent
Intent object provides us many functionalities
- Starting another activity (eg., Login activity to MainActivity)
- Starting background service(eg., Download large files that run on background)
- Sending a Broadcast (eg., Firebase Messages to any alive activity)
Intent types
Explicit Intent
Calling fully qualified targets like starting another activity within an app or starting a download progress by calling a service
Implicit Intent
It's like sharing a content or to ask another application to perform certain actions eg., opening a browser, opening images, playing a video by choosing player like MX player or vlc player or default Videos player
Share Image
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type = "image/jpeg"
shareIntent.putExtra(Intent.EXTRA_STREAM,Utitlities.geturi("apple",this))
Share Multiple Images
val imageUris = ArrayList<Uri>()
imageUris.add(Utitlities.geturi("android", this)) // Add your image URIs here
imageUris.add(Utitlities.geturi("windows", this))
imageUris.add(Utitlities.geturi("apple", this))
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND_MULTIPLE
shareIntent.type = "image/*"
Explanation
Share Text
| val sendIntent = Intent() sendIntent.action = Intent.ACTION_SEND sendIntent.type = "text/plain" sendIntent.putExtra(Intent.EXTRA_TEXT, "http://google.com/careers") startActivity(Intent.createChooser(sendIntent, "send to ")) |
Share Image
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type = "image/jpeg"
shareIntent.putExtra(Intent.EXTRA_STREAM,Utitlities.geturi("apple",this))
startActivity(Intent.createChooser(shareIntent, ""))
Share Multiple Images
val imageUris = ArrayList<Uri>()
imageUris.add(Utitlities.geturi("android", this)) // Add your image URIs here
imageUris.add(Utitlities.geturi("windows", this))
imageUris.add(Utitlities.geturi("apple", this))
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND_MULTIPLE
shareIntent.type = "image/*"
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris)
startActivity(Intent.createChooser(shareIntent, "Share images to "))Explanation
- Creating an intent object
- Setting what intent want to do
- Setting share types like image or video or text
- Data to share
- Start a share
GitHub: https://goo.gl/ZA7hAQ
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_TEXT, "http://google.com/careers")
sendIntent.type = "text/plain"
startActivity(Intent.createChooser(sendIntent, "send to "))

Comments
Post a Comment