我尝试了很多方法,但我做不到。
我有一个 *.txt 文件。我想通过蓝牙、wifi、电子邮件和......
分享它。
当我使用此代码时,我无法共享文件:
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)进行共享时。该文件必须是新电子邮件的附加文件 ...
我找到了这个链接 https://stackoverflow.com/a/16036249/4016922
但是它共享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"));
关于android - 如何在 android 中共享 *.txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42571508/