How to share file or text on gmail, whatsapp, sms, Bluetooth from your Android App (share button)?

This video shows how to create your Android App to share a pdf file over other file sharing Apps such as gmail (or any other email client), whatsapp (or other messaging Apps), google drive, Bluetooth, etc.

This video also shows how a text message can be shared similarly on other mobile App such as gmail/ email, whatsapp, sms, bluetooth.

This App development takes a very simple layout. It just adds a share button each for file sharing and text sharing.

A sample Test.pdf file is already downloaded and kept in the external storage directory for the demo purpose.

In the code, it first takes the permission to access the file from the App. For that the respective permissions are defined in the Manifest file and then in the Java code the permissions are requested from the users explicitly.

StrictMode VmPolicy is build locally in the App to avoid the OS level interruption in sharing the file outside of the parent App.

For sharing the file and text separate methods are written which is accessed as onClick attribute of the two share buttons in the layout.

The code of both the share button remains more or less same as they use intent to open the chooser to send the file or text respectively.

The complete source code of this App is shared below.

We hope you liked this video. for any suggestions, queries or appreciation we can also be reached at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Source Code:

package com.programmerworld.sharebuttonmyapp;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

public class MainActivity extends AppCompatActivity {
private String stringFile = Environment.getExternalStorageDirectory().getPath() + File.separator + "Test.pdf";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}


public void buttonShareFile(View view){
File file = new File(stringFile);
if (!file.exists()){
Toast.makeText(this, "File doesn't exists", Toast.LENGTH_LONG).show();
return;
}
Intent intentShare = new Intent(Intent.ACTION_SEND);
intentShare.setType("application/pdf");
intentShare.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file));
startActivity(Intent.createChooser(intentShare, "Share the file ..."));
}

public void buttonShareText(View view){
Intent intentShare = new Intent(Intent.ACTION_SEND);
intentShare.setType("text/plain");
intentShare.putExtra(Intent.EXTRA_SUBJECT,"My Subject Here ... ");
intentShare.putExtra(Intent.EXTRA_TEXT,"My Text of the message goes here ... write anything what you want");

startActivity(Intent.createChooser(intentShare, "Shared the text ..."));
}
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.programmerworld.sharebuttonmyapp">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

<?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">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="107dp"
android:onClick="buttonShareFile"
android:text="@string/share_file"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="140dp"
android:layout_marginTop="104dp"
android:onClick="buttonShareText"
android:text="@string/share_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

<resources>
<string name="app_name">Share Button My App</string>
<string name="share_file">Share File</string>
<string name="share_text">Share Text</string>
</resources>

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

defaultConfig {
applicationId "com.programmerworld.sharebuttonmyapp"
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

Leave a Reply

%d bloggers like this: