8

We can open share dialog using ACTION_SEND to share text

     Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "Download Link: Android play store link");
                sendIntent.setType("text/plain");
                startActivity(Intent.createChooser(sendIntent, "Share This App"));

How can I use ACTION_SEND to share a text file.

I read http://developer.android.com/training/sharing/send.html but could not get how to share text file.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295

1 Answers1

10

Use the following line.

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
    emailIntent.setType("*/*");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"me@gmail.com"}); 
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
    "Test Subject"); 
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
    "go on read the emails");     
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromfile(new File(yourtextfilepath));
    startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Make sure that your text file path should be from external memory card. Action send wont accept the files from internal memory.

Hope this will help you.

itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
  • 1
    This won't work accross different profiles. You should share content with a ContentProvider instead http://developer.android.com/training/enterprise/app-compatibility.html#sharing_files – rds Feb 09 '15 at 12:08
  • 2
    ... Uri.fromfile( ... syntax is wrong, change it to Uri.fromFile( – Alexander Skvortsov Feb 26 '16 at 10:42
  • 1
    This is the cleanest and simpliest way that I found after several attems and others Examples. My case: My app creates a .csv file and stores it in the DOWNLOAD folder, and i Want share it after the file is created without use of the FileProvider. – Eduardo Corona Jul 23 '17 at 21:19
  • But, can't we share a text file to other apps without using the email route? This looks like it gets intercepted by mail apps only. – JWL Oct 12 '20 at 17:32