Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

Sharing App Data in Android

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

The ability of Android apps to connect and integrate with one another is one of its most appealing features. In this blog, we will be talking about how to share data of our built-in app with other android users, Android Sharesheet, Android intent resolver, MIME types, and much more.
Before jumping directly on how to do this, first knowing the basic knowledge of what is intent and how to use it is strongly recommended.

So, let us start reading about how to share our simple data (like text, images, and files) between different users with two possible methods.

To know about android operating system click here

Sharing Data

Intents and their accompanying extras are used by Android to allow users to quickly and simply share information using their preferred apps.
Users on Android have two options for sharing data between apps:

  1. Android Sharesheet
  2. Android intent resolver 

Android Sharesheet

The Android Sharesheet is mostly used to communicate content outside of your app and/or to another user directly. For instance, sending a URL to a different android user.

Why use Android Sharesheet?

To ensure uniformity for consumers across apps, Android's official documentation strongly advises everyone to use the Android Sharesheet. With a simple swipe, Android Sharesheet allows users to share information with the appropriate person and receive app recommendations. The Sharesheet can recommend targets that aren't available through specialized solutions, and it can do so in a consistent manner. This is due to the Sharesheet's ability to incorporate app and user activity data that is only available to the system.

For developers, the Android Sharesheet includes/provides a number of other useful features, like:

  • Adding our own custom targets: Up to two ChooserTarget objects can be specified in the Android Sharesheet to appear before the sharing shortcuts and ChooserTargets loaded from ChooserTargetServices.
  • Excluding some specific targets: Sharesheet enables us to exclude some specific targets only by providing Intent.EXTRA_EXCLUDE_COMPONENTS. This should only be used to remove targets that users have control over. A general use case can be like when users share from within your app; it's typical to hide your app's share targets because their aim is likely to be to share outside your app.

Android Intent Resolver

The Android intent resolver excels at transferring data to the next stage of a well-defined activity. For instance, Users might open a PDF from their app and let them choose their preferred viewer(like google pdf viewer, drive pdf viewer, or WPS office).

Why use Android Intent Resolver?

When delivering data to another app as part of a well-defined task flow, the Android intent resolver is the best choice. The difference between it and Sharesheet syntax is that it does not need Intent.createChooser(); this is something we will be covering soon with working examples. Also, if you have numerous programs installed that have filters that match ACTION SEND and the MIME type, The system presents an intent resolver disambiguation window, which allows the user to select a target to share with, and If just one application matches, it will be executed.

 

After deciding which type of method to choose(Sharesheet or intent resolver), the next step is to know about ACTION_SEND and MIME

ACTION_SEND

Whenever we create an intent, we must tell it what kind of action we want it to take. The action ACTION_SEND is used by Android to communicate data from one activity to another, even across processes. We must define the data as well as its kind. The system automatically recognizes and shows compatible activities that can receive the data and show it to the user. If only one activity can handle the Intent in the case of the intent resolver, that activity begins immediately.

MIME

For the data we're sending, we should choose the most specific MIME type. When sharing plain text, for example, we should use text/plain. Later on, in this blog, we will see an example of this plain/text. When transferring simple data in Android, there are a few common MIME types like:
text/plain, text/jsontext/html for text/* (plain text ) type data.
image/jpg, image/gifimage/png for image/* (images and gif's) type data.
video/mp4video/3gp for video/* (videos) type data.

Example

So far, we have covered all the theory concepts needed for creating a simple data share app. Now let us see the practical working of it.

Using Sharesheet:

Code:

MainActivity.java
package com.codingninjas.sharedata;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void shareBttnWorking(View view) {
        Intent shareDataIntent =   new Intent();
        shareDataIntent.setAction(Intent.ACTION_SEND);
        String shareDataUrl = "https://play.google.com/store/apps/details?id=in.codingninjas.app";
         shareDataIntent.putExtra(android.content.Intent.EXTRA_TEXT,shareDataUrl);
         shareDataIntent.setType("text/plain");
        startActivity(Intent.createChooser(shareDataIntent, "Share Using"));
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView1"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/share_app_data_using_sharesheet"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/shareBttn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/share_this"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:padding="15dp"
        android:onClick="shareBttnWorking"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

    <TextView
        android:id="@+id/textView2"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/code_studio"
        app:layout_constraintBottom_toTopOf="@+id/shareBttn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />
</androidx.constraintlayout.widget.ConstraintLayout>

Output:

                                            

Here we created an intent shareDataIntent, set it's working to ACTION_SEND, passed url with data type format(plain/text) and started the intent with Intent.createChooser and now we can easily share our data(desired format) with data type supportable app. 
Also, let's use the same type of data to share with Intent Resolver to see the difference.

Using Intent Resolver: 

Code:

MainActivity.java
package com.codingninjas.shareusingintentresolver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void shareBttnWorking(View view) {
        Intent shareDataIntent = new Intent();
        shareDataIntent.setAction(Intent.ACTION_SEND);
        String shareDataUrl = "https://play.google.com/store/apps/details?id=in.codingninjas.app";
        shareDataIntent.setType("text/plain");
        shareDataIntent.putExtra(Intent.EXTRA_TEXT, shareDataUrl);
        startActivity(shareDataIntent);
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/share_app_data_using_intent_resolver"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/shareBttn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/share_this"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:padding="15dp"
        android:onClick="shareBttnWorking"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

    <TextView
        android:id="@+id/textView2"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/code_studio"
        app:layout_constraintBottom_toTopOf="@+id/shareBttn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />
</androidx.constraintlayout.widget.ConstraintLayout>

Output:
                              


Here also first we created an intent shareDataIntent, set it's working to ACTION_SEND, passed url with data type format(plain/text) and started the intent without any Intent.createChooser to share the data.
Here, Using Intent Resolver not just helped us to share our data, but also provided us with the tag of “Remember my choice” for future similar data type sharing references.

Check this out : usb debugging

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

  1. What could be the modifications for adding images instead of plain/text?
    First parse the string URL to URI using Uri myUri = Uri.parse("my_image_url") and then add mime type as image/jpg.
     
  2. What is the difference between Sharing app data Content provider?
    Sharing app data(plain/text, image, media files) can be done using an action send listener, but for sharing large scale complex data like governing access to a central data store.
     
  3. What is the difference in syntax of sharing data with Sharesheet and Intent Resolver?
    Sharesheet uses Intent.createChooser() to generate custom messages while sharing. On the other hand, Intent Resolver does not use such a thing.

Key Takeaways

Cheers if you reached here! 

The purpose of this article was to introduce the reader to the data sharing feature in Android and give some basic idea about how to choose(between Sharesheet and Intent Resolver), initialize, and use them practically in our code.
Head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications. 
We hope this article has helped you enhance your knowledge in Sharing app data on Android. If you want to learn how to set up the environment and architecture of android, check out our article on Kotlin Architecture and Environment Setup. Do upvote this article to help other ninjas grow.

Topics covered
1.
Introduction
2.
Sharing Data
2.1.
Android Sharesheet
2.2.
Android Intent Resolver
2.3.
ACTION_SEND
2.4.
MIME
2.5.
Example
2.5.1.
Using Sharesheet:
2.5.2.
Using Intent Resolver: 
3.
FAQs
4.
Key Takeaways