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

标签 android android-file android-sharing

我尝试了很多方法,但我做不到。

我有一个 *.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/

相关文章:

Android 与 Facebook SharePhotoContent 分享图片 URL

android - 如何在 viber/instagram 上分享图片+文字?

android - 无法接收分享图片 - Android 6.0

安卓错误记录

android - Flutter Firestore 不会检索已发布的 Android apk 中的任何数据

android - 未能获取 FirebaseDatabase 实例。在 FirebaseApp 中指定 DatabaseURL

android - 单击我的按钮时如何共享我的屏幕转换?

android - 无法在 Android 内部存储中写入文件

android - 有没有办法在 Android 上隐藏/混淆 .realm 文件?

android - 错误消息 "qemu-system-i386.exe has stopped working"