Android 将文件附加到 GMAIL - 无法附加空文件 [英] Android Attaching a file to GMAIL - Can't attach empty file

查看:36
本文介绍了Android 将文件附加到 GMAIL - 无法附加空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序总是将相同的文件附加到 GMAIL(撰写>附加文件>打开自>我的程序").它总是会选择同一个文件.

I had a program that would always attach the same file to GMAIL (Compose > Attach File > Open From > "MyProgram"). It would always select the same file.

它在做什么:

String path = Environment.getExternalStorageDirectory() + "/file.3gp";
File f = new File(path);
Uri data = Uri.fromFile(f);
Intent i = new Intent();
i.setData(data);
setResult(Activity.RESULT_OK, i);
finish();

这在 Android 6.0 之前一直运行良好.现在,我在尝试使用它时收到以下错误:

This was working fine until Android 6.0. Now, I receive the following error when trying to use it:

无法附加空文件

Astro 文件共享给了我同样的错误(可能是旧版本).

Astro File Sharing is giving me the same error (can be an old build).

但是,我安装了 ES 文件资源管理器,当我执行相同的例程并选择文件时,我收到一个对话框,内容为:

However, I installed ES File Explorer, and when I do the same routine, and select the file, I receive a Dialog which says:

提取文件为

  • 正常的 Android 方式(用于彩信、Gmail 等)
  • 文件方式(如果上述方法失败,请尝试此方法)

文件方式"会像我的程序一样失败.Normal Android Way"可以正常工作.

The "File Way" will fail as my program does. The "Normal Android Way" will work fine.

有没有人知道它的作用,所以我可以复制?

Does anyone have any idea on what it does, so I can replicate?

提前致谢!

OBS:已经多次尝试 putExtra(STREAM, path),但没有成功.

OBS: Already tried the putExtra(STREAM, path) a lot of times, but without success.

推荐答案

好的,经过大量研究和拦截一些 Intent 后,现在可以使用了.

Ok, got it to work now, after a lot of research and intercepting some Intents.

我必须做的是将 file:///更改为 content://.

What I had to do was change the file:/// to content://.

我按照来自 Android 的这些信息执行此操作:https://developer.android.com/reference/android/support/v4/content/FileProvider.html

I did this following this information from Android: https://developer.android.com/reference/android/support/v4/content/FileProvider.html

唯一的主要变化是我使用了/sdcard/file.ext 的硬编码路径.此外,该行

The only major change was that I used a hard-coded path to /sdcard/file.ext. Also, the line

getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);

改为

Uri contentUri = FileProvider.getUriForFile(this, "com.mydomain.fileprovider", newFile);

还必须包括:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
i.setData(contentUri);

我真的不明白为什么我必须将从文件更改为内容,但在此之后,文件现在又被附加了!如果您遇到此问题,请查看链接,不要忘记需要创建的新 .xml.

I do not really understand why I had to change from File to Content, but after this, the file is now being attached again! See the link if you face this issue, and don't forget about the new .xml that needs to be created.

这篇关于Android 将文件附加到 GMAIL - 无法附加空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆