8

I tried many ways but I can't do this.

I have a *.txt file. I want to share it via Bluetooth, wifi, email and ....

When i used this code i cant share the file:

  File file = new File(Environment.getExternalStorageDirectory(), "Email-Ghap/Emails.txt");
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("*/txt");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, file);
        startActivity(Intent.createChooser(sharingIntent, "share file with"));

Totally I want this: when the user clicked on the share button and chooses one email sender like Gmail for sharing. the file must be attached file for the new email ...

I found this link https://stackoverflow.com/a/16036249/4016922

But it shares txt file content. I want to share the file not the content of the txt file

Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33
SAYE
  • 1,247
  • 2
  • 20
  • 47
  • Try with this links : sharing pDF file you can change with .txt http://stackoverflow.com/questions/17985646/android-sharing-files-by-sending-them-via-email-or-other-apps – Chetan Joshi Mar 03 '17 at 06:08

1 Answers1

13

Change your Code Like this.

From

File file = new File(Environment.getExternalStorageDirectory(), "Email-Ghap/Emails.txt");

To

File file = new File(Environment.getExternalStorageDirectory() + "/" + "Email-Ghap/Emails.txt");

from:

sharingIntent.setType("*/txt");

To

sharingIntent.setType("text/*");

so yourFinal Code Looks Like

    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"));
Learning Always
  • 1,563
  • 4
  • 29
  • 49
  • Thanks, but this can not send file . the Bluetooth and other methods of sharing is appear but when i choose them to send Nothing happens . – SAYE Mar 03 '17 at 07:19
  • When i use this `File file = new File(Environment.getExternalStorageDirectory() + "/" + "Email-Ghap/Emails.txt");` i print `file` in log . its not true address . i think my way to find path is true : `File file = new File(Environment.getExternalStorageDirectory(), "Email-Ghap/Emails.txt");` – SAYE Mar 03 '17 at 07:22
  • actually from same code I was able to send my file. – Learning Always Mar 03 '17 at 07:26
  • are you sending a `*.txt` file ? can you send to `Bluetooth` successfully ? – SAYE Mar 03 '17 at 07:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137118/discussion-between-janki-and-abbas-nikzad). – Learning Always Mar 03 '17 at 07:49
  • Uri.parse gives an error on "parse" as called the error: error: cannot find symbol method parse(String) what should I do? – Bay Sep 06 '19 at 06:47