从我的应用添加Gmail中安全存储空间中的文件

时间:2011-04-30 18:38:03

标签: android gmail android-intent attachment

我的应用程序可以选择发送日志,该日志存储在应用程序的安全存储中。该文件的路径是“/data/data/com.mycompany.myapp/files/log.zip”。该文件的权限已被设置为MODE_WORLD_READABLE,启动电子邮件的意图是:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("application/zip");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///data/data/com.mycompany.myapp/files/log.zip));
startActivity(Intent.createChooser(i, "Send Error Log"));

如果文件位于SD卡上,则没有问题。但是当它是安全存储时,就没有附件。我很确定这不是预先提交的问题,因为它与股票电子邮件客户端和TouchDown Exchange应用程序完美配合。只有Gmail才有这个问题。这是Gmail中的一个小故障,还是我错过了什么?

感谢。

4 个答案:

答案 0 :(得分:2)

是的,我们可以使用FileProvider将存储在files目录中的内部文件附加到Gmail。使用FileProvider我们可以临时访问我们应用程序的一些内部文件(如filepaths.xml中所述)

在Android文档中提到的清单中添加一个FileProvider:

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

现在在你的app的res / xml文件夹中创建filepaths.xml,并添加以下代码:

 <paths>
 <files-path path="." name="name" />

注意:这将允许访问根文件目录,如果您想要对内部存储中的某些子目录(例如图像)进行特定访问,请将路径称为“images /”

    <paths>
    <files-path path="images/" name="name" />

在代码中:

File file=new File(context.getFilesDir(),"test.txt");

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);

shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                                 "Test");

shareIntent.setType("text/plain");

shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                             new String[] {"email-address you want to send the file to"});

Uri uri = FileProvider.getUriForFile(context,"com.package.name.fileprovider",
                                               file);

            ArrayList<Uri> uris = new ArrayList<Uri>();
            uris.add(uri);

            shareIntent .putParcelableArrayListExtra(Intent.EXTRA_STREAM,
                                                    uris);


            try {
               context.startActivity(Intent.createChooser(shareIntent , "Email:").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));                                                      


            }
            catch(ActivityNotFoundException e) {
                Toast.makeText(context,
                               "Sorry No email Application was found",
                               Toast.LENGTH_SHORT).show();
            }
        }

这对我有用。希望这有助于:)

答案 1 :(得分:1)

没关系,我找到了答案 - Gmail不允许任何附件,除非它们来自SD卡。我最终不得不将文件复制到外部存储cachce,然后一切正常。很糟糕,即使权限正确,Gmail也会随意决定它不会在内存存储上使用文件!

答案 2 :(得分:0)

Gmail客户端应用程序附件终于可以在Android 4.0+设备上运行。我已经在我的4.2和4.3设备上进行了测试,但它们都能正常工作。

我已经通过我的moto razr 2.3验证了gmail客户端不起作用。使用另一个邮件客户端工作正常,所以你在2011年的答案是正确的。

答案 3 :(得分:0)

的AndroidManifest.xml

        <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.cummins.faultadvisor.LogFileShare"
        android:exported="false"
        android:grantUriPermissions="true" >
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/log_file_provider" />
    </provider>

RES / XML / path.xml

     <?xml version="1.0" encoding="utf-8"?>
     <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="files" path=""/>
    </paths>

您的活动文件应具有以下意图:

    Uri log_file_uri = null;
    Uri logcat_file_uri = null;
    String log_filePath = activity.getApplicationContext().getFilesDir().getPath()
    + "/"+ activity.getApplicationContext().getResources().getString(R.string.log_file);
    String logcat_filePath = activity.getApplicationContext().getFilesDir()
    .getPath()+ "/"+ activity.getApplicationContext().getResources().getString(R.string.logcat_file);
    File log_file = new File(log_filePath);File logcat_file = new File(logcat_filePath);
    if (log_file.exists() && logcat_file.exists()) {
    log_file_uri = FileProvider.getUriForFile(
    activity.getApplicationContext(),"com.example.LogFileShare",log_file);
    logcat_file_uri = FileProvider.getUriForFile(
    activity.getApplicationContext(),"com.example.LogFileShare",logcat_file);