About media sharing

Whether it's a funny image, an informative video, or a captivating audio clip, enabling users to share content enriches their experience and promotes engagement. This document explores the essentials of sharing media on Android, including the APIs and techniques you need to integrate this capability.

Designed for sharing

Android's design promotes interoperability between apps using a system based on Intents. An Intent is an abstract description of an operation to be performed. Intents allow apps to communicate with each other without having to know specifics about each other.

When an app wants to share data or initiate an action, it creates an Intent that specifies the type of content and operation. The Android system presents a list of relevant apps that can handle that Intent, allowing the user to choose which app to use. This approach fosters a collaborative ecosystem.

Sharing text involves just a few lines of code:

val sendIntent: Intent = Intent().apply {
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, "This is my text to share.")
    type = "text/plain"
}

val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)

The createChooser line displays the Android Sharesheet UI, which lets users share information with people — including relevant app suggestions — with a single tap. Other things you can do with the Android Sharesheet include:

See Send simple data to other apps for more information about Android Sharesheets and how to use them.

Provide Direct Share targets to make it easier and faster for users of other apps to share URLs, images, or other kinds of data with your app. Direct Share works by presenting contacts from messaging and social apps directly on the Android Sharesheet, without users having to select the app and then search for the contact.

Support receiving rich content through an OnReceiveContentListener. This API provides a single place for your code to handle receiving all content, from plain and styled text to markup, images, videos, audio files, and others. The content can come from image keyboards, drag and drop, or the clipboard.

Share media files

Intents can only contain a small amount of data, so Android provides a way for Intents to contain a secure handle to files. Sharing media files securely from your app involves:

See About sharing files for more information about how to securely share files.

Optimize media for sharing

Whether you're sharing media to other users in your app, or sharing media to another app, you want to make sure you share media that offers a high quality sharing experience.

Striking the balance between quality and size

Large media files can quickly consume bandwidth and storage, leading to frustrating delays and potential data overage charges for your users. Compression is your best friend here.

  • Image compression: Utilize modern image compression formats like WebP and AVIF, which offer superior compression ratios compared to traditional JPEGs without significant quality loss. Experiment with different quality settings to find the sweet spot.
  • Video compression: Leverage the power of AV1 or H.265 (HEVC) video compression to provide better compression efficiency while maintaining excellent visual quality. You can check for the presence of hardware encoding on Android 10+ devices, as well as the mediaPerformanceClass to help determine what your device can best support. Consider offering different resolution options to cater to varying user preferences and network conditions.
fun hasHardwareEncodingSupportFor(mimeType: String): Boolean {
    val codecList = MediaCodecList(REGULAR_CODECS)
    val codecInfos = codecList.codecInfos
    for ( codecInfo in codecInfos ) {
        if (!codecInfo.isEncoder()) {
            continue;
        }
        if (!codecInfo.isHardwareAccelerated()) {
            continue;
        }
        val types: Array<String> = codecInfo.getSupportedTypes()
        for (j in types.indices) {
            if (types[j].equals(mimeType, ignoreCase = true)) {
                return true
            }
        }
    }
    return false
}
// test for AV1 hardware encoding support
val hasAV1 = hasHardwareEncodingSupportFor("video/av01")

Adapting media

Social media platforms often enforce specific dimensions and aspect ratios for shared media. By proactively resizing and cropping media files before sharing, you can avoid unexpected distortion or formatting issues when users post to their favorite platforms.

Provide clear instructions and guidance on how users can optimize their media before sharing. This could include tips on adjusting encoding bitrates, setting quantization parameters, choosing the video format, selecting appropriate file sizes, or understanding the impact of different sharing options.

Enhancing media discoverability

Adding relevant metadata, such as titles, descriptions, and tags to your media files can improve their discoverability. Encourage users to add their own descriptions and captions when sharing, further personalizing the experience.

Add metadata to images

The Jetpack ExifInterface class writes metadata into JPEG, PNG, and WebP images in the form of Exif tags.

// sets the title of the image in the form of Exif data
val exif = ExifInterface(imageFile)
exif.setAttribute(ExifInterface.TAG_IMAGE_DESCRIPTION, "Beautiful sunset")
exif.saveAttributes()