Android App Links allow your application to open web links instead of a web browser. Handling these deep links has been covered in our Deep Links Crash Course. In addition to being able to open deep links, your app should be able to create and share deep links to specific content as well.
Sharing links to content inside of your application is an important action in Android. Link sharing enables your app and its users to engage in collaboration, communication, social networking, etc. All of these activities increase engagement with your application, and give your application’s deep linking support a proper workout. A great tool for sharing linked content is the Android Sharesheet. This article will show why you should use the Sharesheet and demonstrate how to use it with linked content. Let’s get started!
First, what is the Sharesheet? Android provides an in-app interface for selecting users or applications to send content directly to. This interface is called the Sharesheet, which displays when you call Intent.createChooser using an intent with the ACTION_SEND action. The Sharesheet provides an effective way to share data with other apps without using explicit intents.
The following example will invoke the Sharesheet, presenting users with relevant targets to share to. Your users will each have a sheet tailored to their installed applications and contacts.
This is a basic example, and we’re much more likely to be sharing something more interesting than plain text. If your application supports Android App Links, it can share links directly to content inside of the application. Let’s look at a sample link: “https://example.com/specials/spaghetti“. This link is clear to users of the app that it is a spaghetti special, and we could probably get by without spicing up the share sheet.
However, sometimes a link’s destination might not be clear. A link to a Google Doc, as an example, often looks something like this: https://docs.google.com/document/d/1TejHHDrz…NotARealLink. From the link alone, a user might not know what information they are actually sharing. In this case, we should consider adding a richer preview.
Consider sharing a complicated URL like: https://www.google.com/search?ei=2rRVXcLkJajM0PEPoLy7oA4. A richer preview can reassure your users what is being shared.
For our use, a richer preview includes extra title text and an image thumbnail. We can set our title text by adding a description to Intent.EXTRA_TITLE. To embed images in the Sharesheet, we set the Intent’s clipData parameter to a content URI that is served by a file-provider. The rest of this article will walk you through providing richer share content previews in the Sharesheet by embedding images from an arbitrary URL.
First: Let’s create a file-provider in our AndroidManifest.xml file.
Now we need to add the filepaths.xml file to our XML resources directory. For our example we will provide images from the root or the cache directory. This example filepaths.xml file will link our cache root to the content uri for “/images”.
While not strictly necessary, according to the FileProvider API docs we should create a stub implementation for our application. Quote, “It is possible to use FileProvider directly instead of extending it. However, this is not reliable and will cause crashes on some devices.”
So what has this actually done? We have declared a file provider that can make a content URI for images in the root of our cache directory. We can call FileProvider.getUriForFile to create a URI that Sharesheet can use to display the image.
With a link, title, and content Uri to our image, we are now ready to make our intent.
And voila! The Sharesheet with rich text previews.