【问题标题】:Problem Attaching internal file to GMail in my android app在我的 android 应用程序中将内部文件附加到 GMail 的问题
【发布时间】:2014-12-04 12:43:56
【问题描述】:

我的应用程序将数据写入文本文件(在 SD 卡和内部存储器上)。 稍后,该应用程序会将文本文件通过电子邮件发送给人员列表。 我无法让 gmail 附加从内部应用程序文件区域提取的文件。 “原生 Android 邮件”可以毫无问题地从内部或 SD 卡区域附加文件。如果文件来自 SD 卡,Gmail 会附加文件,但如果文件位于内部存储中,则不会附加文件。

// 这会从 SD 发送文件 - 适用于 android 邮件和 gmail

Intent jj=new Intent(android.content.Intent.ACTION_SEND);

String fileName = "file://" + Environment.getExternalStorageDirectory()+"/aFolder/externalfile.txt"           

jj.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileName));
jj.setType("text/plain");

Intent chooser = Intent.createChooser(jj , "Select Sender");
startActivity(chooser);

// 这会发送一个内部文件,适用于 android 邮件,但没有使用 gmail 发送附件

Intent jj=new Intent(android.content.Intent.ACTION_SEND);

String fileName = "file://" + getFilesDir().toString() + "/internalfile.txt";

jj.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileName));
jj.setType("text/plain");

Intent chooser = Intent.createChooser(jj , "Select Sender");
startActivity(chooser);

有什么建议吗? 我是否需要以某种方式授予 Gmail 特殊权限?
我的附件都是文本文件 - 由应用程序编写。
内部文件是使用 openFileOutput(myFile,32769)

创建的

谢谢 约翰·D

【问题讨论】:

  • Gmail 意图不允许从内部存储发送(放入附件)文件。所以你必须只使用外部存储器:(
  • pankaj - 你能指出任何证实这一点的文件吗?
  • 我在 logcat 中收到以下消息:ERROR/Gmail(9147): file:// 附件路径必须指向 file:///mnt/sdcard。忽略附件 file:///data/data/my.package.name/files/myfile.txt 我应该如何解释这个? - 我永远不能将内部存储器文件附加到 GMail 中吗? - 我需要更改权限吗? - 我需要以不同的方式构建我的 URI 吗?
  • 同样的问题。我无法将文件附加到 Gmail,但可以使用其他电子邮件客户端。

标签: android


【解决方案1】:

我发现的唯一方法是创建自己的内容提供者并将 uri 作为附件传递给我的内容提供者。

【讨论】:

    【解决方案2】:

    和 Dhego 一样,我使用了内容提供商。具体来说,一个 FileProvider。

    https://developer.android.com/reference/android/support/v4/content/FileProvider.html

    使用它只需要你修改你的清单并创建一个额外的资源文件。此外,您还需要通过 FileProvider 提供的静态方法获取格式有效的 URI。

    在你的 Manfiest:

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.authority.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
         </provider>
    

    “com.example.authority.fileprovider”是您的应用程序授权,附加了“fileprovider”。

    在 res/xml 文件夹中,创建一个 file_paths.xml 文件,其中包含您要公开的文件的路径。就我而言,我是从应用程序缓存目录中公开它们,所以我的 XML 看起来像:

        <paths xmlns:android="http://schemas.android.com/apk/res/android">
            <cache-path name="reports" path="reports/"/>
        </paths>
    

    在我的例子中,“reports”是应用程序缓存目录中的一个文件夹,我正在向其中写入文件。

    最后要做的是在你的代码中:

    1. 将要公开的文件写入 file_paths.xml 中指定的文件夹和存储区域。
    2. 生成一个有效的 URI 以设置在您将调用以发送电子邮件的 Intent (Intent.ACTION_SEND)。​​

    这里有一些示例代码:

    emailIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getActivity(), "com.example.authority.fileprovider", fileToAttach));
    

    在你的 Intent 上调用 startActivity 就可以了!

    【讨论】:

      【解决方案3】:

      看来确实有an issue with gmail。然而不幸的是,在撰写本文时,它似乎还没有得到修复。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-25
        • 2011-08-27
        • 1970-01-01
        • 2015-11-25
        • 1970-01-01
        • 2011-06-29
        • 1970-01-01
        相关资源
        最近更新 更多