Open In App

Receive Rich Content in Your App on Android 13

Improve
Improve
Like Article
Like
Save
Share
Report

Android 13 brings to us a plethora of new services, APIs, features, and functions to add to our app to turn the overall user experience more delightful than ever. Starting in this release there have been introductions to even richer content and the inclusion of newer file types which one can include in their apps to get more insights and drive more users towards them. One such feature which has improved and comes with a few new changes is the ability to import rich content directly into your Android apps, one thing which could not be done earlier natively on the platform. In this article, we will be looking at how to increase your productivity and get create a better user experience by letting them add rich content to your apps directly.

Understanding Global Rich Content

Users adore photographs, movies, and other expressive content, but it can be challenging to input and move this content within apps. Android 12 (API level 32) provides a unified API that allows your app to accept content from any source: the clipboard, keyboard, and drag and drop. This made it easier for apps to receive rich content. When content is injected using any method, you can get a callback by attaching an interface called OnReceiveContentListener to UI components. Your code will now handle receiving all material, including plain and styled text, markup, pictures, videos, audio files, and others, in the callback.

Other current APIs include a comparable API for each UI element, such as the long-press menu or drag-and-drop. For each mechanism that adds material, you must separately integrate with each API, adding the corresponding code.

Adding the new API

There are already many methods by which the user can add data to your app, to add the functionality of getting content from everywhere you will have to attach all these new file providers to each of the input methods. We will look at the below diagram for a better understanding of the above-said point.

Understanding APIs supporting Global Content

Image #1: Understanding APIs supporting Global Content

How Android 13 Helps

As you have seen that prior to Android 13 you had to implement a lot of different APIs that needed to work in order to work and make content addition possible using different sources, android 13 now included and merges every method to make it a single entity and all you need is to implement the new API. The figure below explains the new API released, which makes adding Global Content Easier in your Android Apps:

Understanding how Android 13 helps

Image #2: Understanding how Android 13 helps

This method also eliminates the need for extra code changes to allow support in your app when new methods of content insertion are added to the platform. You can still use the current APIs, which will continue to function in the same manner if your app needs to implement complete modification for a specific use case.

How to Implement the new API

The OnReceiveContentListener method of the API is a listener interface. We advise utilizing the corresponding OnReceiveContentListener interface in the AndroidX Core library to support earlier iterations of the Android platform.

Java




public class gfgRichContentRecieverClass
    implements OnReceiveContentListener {
    // Add this method to your 
    // activity where the content is
    // being imported.
    public static final String[] IMPORT_TYPES
        = new String[] { "documents/*", "video/*" };


You can reuse your app-specific code for managing content URIs if your app already supports sharing with Intents. Any remaining data should be returned to the platform can handle it.

Set the listener on the necessary UI elements in your app after implementing it:

Java




public class gfgActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // Add this to your main activity component to get
        // global content. Rich content comes here.
        AppCompatEditText gfgInputField
            = findViewById(R.id.richImportTextGeek);
        ViewCompat.setOnReceiveContentListener(
            gfgRichInput, MyReceiver.MIME_TYPES,
            new gfgCustomLatch());
    }


In a service or activity, your program should typically process content URIs. Use WorkManager for processing that takes a while. By bypassing the content using Intent while implementing this, you should extend rights to the target service or activity. Using the setClipData command with the FLAG_GRANT_READ_URI_PERMISSION flag. As an alternative, you can process the content in the current context using a background thread. To prevent rights from being revoked by the platform too soon in this scenario, you must keep a reference to the payload object received by the listener. For any content URIs in the payload supplied to the OnReceiveContentListener, the platform immediately grants and releases read access.

Conclusion

The OnReceiveContentListener API can be viewed as the keyboard image API’s replacement. The keyboard image API’s features as well as some new ones are supported by this unified API. Hope this article helped you learn how to get globally rich data directly in your Android App, adding this would surely enhance user experience as they would be able to add any data quickly and reliably.



Last Updated : 06 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads