How to share *.txt file in android

徘徊边缘 提交于 2019-12-06 04:25:56

问题


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

I have a *.txt file . i want 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 user clicked on share button and choose one email sender like Gmail for sharing . the file must be attached file for new email ...

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

But it share txt file content . i want to share file not content of txt file


回答1:


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"));


来源:https://stackoverflow.com/questions/42571508/how-to-share-txt-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!