如何在 android 中共享 *.txt 文件

How to share *.txt file in android

我试了很多方法都做不到。

我有一个 *.txt 文件。我想通过 Bluetooth, wifi, email and ....

分享

当我使用此代码时,我无法共享文件:

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

我完全想要这个:当用户点击共享按钮并选择一个电子邮件发件人(如 Gmail 进行共享时。该文件必须是新电子邮件的附件...

我找到了这个link

但是它共享txt文件内容。我要分享的是文件而不是txt文件的内容

像这样更改您的代码。

来自

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

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

来自:

sharingIntent.setType("*/txt");

sharingIntent.setType("text/*");

所以你的最终代码看起来像

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