0

I have written code to share .txt file on my device to whatsapp. However text file gets shared but instead of showing the name of the .txt file it shows untitled. Here is my code.I have also attached screen shot of whatsapp screen after sharing the file.

public static void shareTextToSocialMedia(Context c,String sFilePath)
{
    //share pdf via WhatsApp
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    Uri screenshotUri = Uri.parse(sFilePath);

    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
    c.startActivity(Intent.createChooser(sharingIntent, "Share report using"));
}

enter image description here

Goku
  • 9,102
  • 8
  • 50
  • 81
Karthik Pai
  • 587
  • 1
  • 9
  • 18

3 Answers3

1

My issue got resolved.The code is correct.The path was wrong. I added file:// to the path.I set the path of .txt file like this, "file://"+ Environment.getExternalStorageDirectory()+ File.separator+"Notes"+File.separator+sName+".txt"; View attached image

Karthik Pai
  • 587
  • 1
  • 9
  • 18
1

The best way to do it is using FileProvider as this can lead to FileUriExposedException

You can refer this post https://stackoverflow.com/a/46761554/2793036

Rishabh876
  • 3,010
  • 2
  • 20
  • 37
1

You try this it will help for you

Share an txt file from your app

File file = new File(Environment.getExternalStorageDirectory().toString() + "/" +   "abc.txt");
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));
startActivity(Intent.createChooser(sharingIntent, "share file with"));

Share Image from your App Example

File f=new File("full image path");
Uri uri = Uri.parse("file://"+f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("image/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(share, "Share image File"));

Share Image directly to WhatsApp from your app

File f=new File("full image path");
Uri uri = Uri.parse("file://"+f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage("com.whatsapp");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("image/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(share, "Share image File"));
Arjun Othayoth
  • 351
  • 3
  • 5