Share Image and Text using android Intent

In this article, we will learn how to share your required data using android intent. This article is part of the **“Today I Learnt”**category.

How intent opens share intent?

The intent is a class that helps to perform different actions like open ACTION_VIEW, ACTION_WEB_SEARCH, ACTION_SEARCH, etc. In this article, we’ll see about the action ACTION_SEND.

The ACTION_SEND helps to open the share intent where you will share your required data. Now we’ll learn how we share text and image using android Intent with the help of share intent. So, Let’s started.

Share text using intent

So, In this section, we’ll share a simple text using intent. So follow this below code.

  val sendIntent = Intent()
  sendIntent.action = Intent.ACTION_SEND
      "Send a simple text"
  sendIntent.type = "text/plain"

Share image using intent

A few days ago I had a task where I wanted to share the image with other apps. I searched many solutions on StackOverflow but those were not cases I wanted. So, I did it by in my own way using Picasso library. Here are the steps to do this.

Step 01: Add Picasso dependency in build.gradle(app)

implementation ‘com.squareup.picasso:picasso:2.71828’

Step 02: Add below code in onCreate() method.

    val builder: StrictMode.VmPolicy.Builder = StrictMode.VmPolicy.Builder()

Before Android 7.0 there were no restriction to share data on other apps but after Android 7.0 Google made some restrictions on data sharing. So, these lines will remove the sharing restrictions.

Step 03: After that, call this function on click listener.

fun shareImageFromURI(url: String?) {
    Picasso.get().load(url).into(object : Target {
        override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
            val intent = Intent(Intent.ACTION_SEND)
            intent.type = "image/*"
            intent.putExtra(Intent.EXTRA_STREAM, getBitmapFromView(bitmap))
            startActivity(Intent.createChooser(intent, "Share Image"))
        override fun onPrepareLoad(placeHolderDrawable: Drawable?) { }
        override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) { }

So, this method will share the image on different apps. It takes the path/url of image and load into Bitmap. Picasso converts it into the bitmap and then we create a method that returns URI from the bitmap. So, the below method returns the URI from a bitmap.

fun getBitmapFromView(bmp: Bitmap?): Uri? {
    var bmpUri: Uri? = null
    try {
        val file = File(this.externalCacheDir, System.currentTimeMillis().toString() + ".jpg")
        val out = FileOutputStream(file)
        bmp?.compress(Bitmap.CompressFormat.JPEG, 90, out)
        bmpUri = Uri.fromFile(file)
    } catch (e: IOException) {
  return bmpUri

For complete code click here

Fewwwwwwww finally done 😃.

Thanks for reading if you want any help then follow me on Github, Twitter.

See also