Sharing in Android 10: Getting Started

In this Sharing in Android 10 tutorial, you’ll learn how to use the Sharing Shortcuts API to receive and share images in an app. By Gordan Glavaš.

Leave a rating/review
Download materials
Save for later
Share

These days, mobile users don’t just want to consume interesting information, they want to share it with their friends as well. Now that many people have internet wherever they go, the need to send and receive text, images and other files from mobile apps has become extremely important.

In this Android 10 tutorial, you’ll learn the best way to share files simply and efficiently, allowing you to add this crucial task to your own apps.

You’ll do this by improving Meme Repo, an app that lets you curate your own repository of funny images, so that you can use the app to easily exchange data.

Along the way, you’ll learn how to:

  • Receive a shared image from another app.
  • Quick share with sharing shortcuts.
  • Share images from your app.

Getting Started

Use the Download Materials button at the top or bottom of this tutorial to download the begin project.

You’ll notice that the entire app is already there. Here’s a quick breakdown of the code, to show you around a bit:

Structure of the begin project

  • MainActivity.kt: Your central activity that displays a grid of meme images organized into tabbed categories. The code that supports this UI lives in MemeCollectionFragment.kt, MemesAdapter.kt and SectionsPagerAdapter.kt.
  • MemeActivity.kt: Allows you to add a new meme to your collection by supplying its image URL, title and category.
  • Meme.kt: The meme’s model class.
  • Category.kt: An enum class that defines a meme category. Currently, the app supports Classic and Dank memes.
  • MemeRepo.kt: A singleton that manages your collection, caching it and making sure it reads from and writes to local preferences.

Build and run. Most of the app already works nicely:

Meme Repo's start screen

You can already add a meme:

Meme Repo with an added meme

However, you’ll soon notice that the app isn’t very easy to use. For example, if you run into an interesting meme while browsing the internet, you have to copy its image link, go to the app, press the + button and then paste the URL into the field.

If that sounds like a hassle. Android sharing is here to save you.

Receiving a Meme From Another App

In order to build your repository, you need a convenient way to send the memes you find while using other apps to Meme Repo. Your first step will be to improve the app by allowing other apps to share images with it.

Open AndroidManifest.xml and add this code inside activity android:name=".activity.MemeActivity"...:

<intent-filter>
  //1
  <action android:name="android.intent.action.SEND" /> 
  //2
  <category android:name="android.intent.category.DEFAULT" />
  //3  
  <data android:mimeType="image/*" />
</intent-filter>

This code adds a new intent-filter that allows other apps to share with Meme Repo:

  1. It sets action to android.intent.action.SEND, which is the norm for sharing.
  2. The category is android.intent.category.DEFAULT.
  3. The shared content is an image. That is, its MIME type corresponds to image/*.

You’ve now exposed your app to other apps for sharing, but you still need to handle the incoming shared content.

Handling Incoming Content

Open MemeActivity.kt and add this at the end of onCreate:

//1
if (intent?.action == Intent.ACTION_SEND) { 
  //2
  if (intent.type?.startsWith("image/") == true) {
    //3
    (intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
       // 4
       memeUri.setText(it.toString()) 
    }
  }
  // TODO: More to come here.
}

This code:

  1. Checks if the Activity started with an intent equal to Intent.ACTION_SEND.
  2. Checks if the shared content is an image.
  3. Checks if the shared image is available in the intent extras.
  4. If the incoming content meets all the conditions, the app pastes the URL of the shared image into memeUri‘s text field.

Build and run.

Try sharing an image from another app to Meme Repo by opening your browser, navigating to an awesome web page and long-pressing an image to share it.

Long-press image menu

Meme Repo now appears as an option alongside some other apps, making it much simpler to build your meme collection.

Meme Repo appears among your sharing options

Using Sharing Shortcuts

Hopefully, Google will fix this soon. In the meantime, please use a real device to test the code from this section.

Note: As of December 2019, the Android 10 emulator has a bug which prevents sharing shortcuts from working properly. When you try to use it, you’ll get the message: Direct Share not available in the share sheet.

Hopefully, Google will fix this soon. In the meantime, please use a real device to test the code from this section.

Now that you’ve eliminated the need to manually switch apps and paste an image URL, your next step is to make it easier for the user to select the meme’s category.

To do this, you’ll use the sharing shortcuts API. If you use any of the popular messaging apps, you’ve seen this API in use when the app offers you a few contacts to directly share content with, without having to manually specify the recipient.

In this case, you’ll add two shortcuts to the sharing screen so that the user can immediately decide if a meme is Classic or Dank. It takes only a few simple steps to do this, starting with creating a file where your shortcuts will live.

Creating a Shortcuts File

Start by adding a new file to res/xml and name it shortcuts.xml. Enter the following code as its content:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  //1
  <share-target android:targetClass="com.raywenderlich.android.memerepo.activity.MemeActivity">
    //2
    <data android:mimeType="image/*" />
    //3
    <category android:name="com.raywenderlich.android.memerepo.category.MEME_STORE_TARGET" />
  </share-target>
</shortcuts>

With this code, you’re specifying that:

  1. The shortcuts will take the user to MemeActivity.
  2. Images are the only supported content for sharing.
  3. The category uniquely identifies your sharing shortcuts.

Now that you have a shortcuts file, you need to link it to your app.

Linking Your Shortcuts File to Meme Repo

For your next step, go to AndroidManifest.xml and add this code in <activity android:name=".activity.MainActivity" ..., just before intent-filter:

<meta-data
  android:name="android.app.shortcuts"
  android:resource="@xml/shortcuts" />

While you’re updating the manifest, add this code in <activity android:name=".activity.MemeActivity" ..., just before intent-filter:

<meta-data
  android:name="android.service.chooser.chooser_target_service"
  android:value="androidx.sharetarget.ChooserTargetServiceCompat" />

Here, you’re ensuring that your app is compatible with previous OS versions.

Organizing Your Sharing Code

Now, it’s time for the juicy part. To keep all the sharing-related code in one place, create a new singleton file in the util package and name it ShareUtil.kt. Put a single constant in it, equal to the category you previously listed in shortcuts.xml.

object ShareUtil {
 private const val SHARE_CATEGORY
     = "com.raywenderlich.android.memerepo.category.MEME_STORE_TARGET"
}

You’ll use this constant in the next function.

Add the following extension method to ShareUtil to transform a Category into a ShortcutInfoCompat:

private fun Category.toShortcut(context: Context): ShortcutInfoCompat {
  val label = context.getString(stringResId)
  //1
  return ShortcutInfoCompat.Builder(context, id) 
       //2
      .setShortLabel(label) 
       //3
      .setLongLabel(context.getString(R.string.new_meme, label)) 
       //4
      .setPerson(Person.Builder()
          .setName(name)
          .setKey(id)
          .build())
       //5
      .setIcon(IconCompat.createWithResource(context, imageResId)) 
       //6
      .setCategories(setOf(SHARE_CATEGORY)) 
       //7
      .setIntent(Intent(context, MemeActivity::class.java).apply { 
        action = Intent.ACTION_SEND
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
          //8
          putExtra(Intent.EXTRA_SHORTCUT_ID, id) 
        }
      })
       //9
      .setLongLived(true) 
       //10
      .setRank(0) 
      .build()
}

There’s a lot going on in that code, so here’s a quick breakdown:

  1. Each shortcut needs a unique ID.
  2. Short label is a user-facing text shown in the share sheet, among other places.
  3. Long label allows for more descriptive text.
  4. Associating a Person is optional, but it improves the shortcut’s ranking. A person in this case is one of two Category enums
  5. Using IconCompat ensures your shortcut icon looks like you intend it to.
  6. Each shortcut has a category. In this case, you declared the category in shortcuts.xml.
  7. This intent triggers when you use quick shortcuts on the app icon.
  8. Android Pie introduced Intent.EXTRA_SHORTCUT_ID. You’ll conditionally set this required key depending on the version of Android. Using this extra allows the receiver Activity to identify the sender category.
  9. The OS caches long-lived shortcuts after they’re unpublished. More on publishing/unpublishing in the next section.
  10. Rank dictates the order of published shortcuts. The lower the ranking, the better.

With the grunt of the work done, you are ready to actually publish the shortcuts.