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:
- Android Sharesheet
- 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/json, text/html for text/* (plain text ) type data.
image/jpg, image/gif, image/png for image/* (images and gif's) type data.
video/mp4, video/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