从我的应用程序附加 Gmail 安全存储中的文件

发布于 2024-11-04 15:16:20 字数 536 浏览 0 评论 0原文

我的应用程序可以选择发送日志,该日志存储在应用程序的安全存储中。文件的路径是“/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 的故障,还是我遗漏了什么?

谢谢。

My app has an option to send out the log, which is store in the app's secure storage. The path to the file is "/data/data/com.mycompany.myapp/files/log.zip". The file's permissions have been chnged to MODE_WORLD_READABLE, and the intent to launch email is:

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

If the file is located on the SD card, there is no problem. But when it's secure storage, there is no attachment. I'm pretty sure that it's not a premissions issue because it works perfectly with the stock email client and with the TouchDown Exchange app. It's only Gmail that has this problem. Is this a glitch in Gmail, or am I missing something?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我喜欢麦丽素 2024-11-11 15:16:20

是的,我们可以使用 FileProvider 将存储在文件目录中的内部文件附加到 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>

现在在应用程序的 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();
            }
        }

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

Yes, we can attach internal files stored in files directory to Gmail using FileProvider.Using FileProvider we can give temporary access to some of our app's internal files(as mentioned in filepaths.xml)

In the manifest as mentioned in the Android documentation add a 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>

Now in your app's res/xml folder create filepaths.xml, and add the following code:

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

Note:This will give access to the root files directory, if you want to give specific access to some subdirectory, say images, in your internal storage mention the path as "images/"

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

In the code:

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();
            }
        }

This worked for me.Hope this helps :)

故事和酒 2024-11-11 15:16:20

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

Nevermind, I found the answer -- Gmail does not allow any attachments unless they come from the SD card. I ended up having to copy the file to external storage cachce, and then everything worked. It sucks that Gmail arbitrarily decides that it won't use files on insternal storage, even if permissions are correct!

流年里的时光 2024-11-11 15:16:20

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

我已经用我的 moto razr 2.3 验证了 gmail 客户端无法工作。使用另一个邮件客户端工作得很好,所以你的答案截至 2011 年是正确的。

Gmail client app attachments finally now works on android 4.0+ devices. i've tested on my 4.2 and 4.3 devices and they work.

I have verified with my moto razr 2.3 that the gmail client does NOT work. using another mail client works just fine, so your answer as of 2011 was correct.

失眠症患者 2024-11-11 15:16:20

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

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>

your activity file should have following intent :

    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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文