繁体   English   中英

尝试将文件从 SD 卡附加到 email

[英]Trying to attach a file from SD Card to email

我正在尝试启动发送 email 的意图。 所有这些都有效,但是当我尝试实际发送 email 时,会发生一些“奇怪”的事情。

这是代码

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("image/jpeg");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Photo");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/dcim/Camera/filename.jpg"));
sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the photo");
startActivity(Intent.createChooser(sendIntent, "Email:"));

因此,如果我使用 Gmail 菜单上下文启动它会显示附件,让我输入 email 是谁,然后编辑正文和主题。 没什么大不了。 我点击发送,它发送。 唯一的问题是附件没有发送。

所以。 我想,为什么不尝试使用 Email 菜单上下文(用于我手机上的备份 email 帐户)。 它显示附件,但正文或主题中根本没有文本。 当我发送它时,附件会正确发送。 那会让我相信有些事情是完全错误的。 我是否需要在 Manifest 启动时获得新许可才能发送带有附件的 email? 我究竟做错了什么?

也遇到同样的问题

代码:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
    emailIntent.setType("image/jpeg");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] 
    {"me@gmail.com"}); 
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
    "Test Subject"); 
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
    "go on read the emails"); 
    Log.v(getClass().getSimpleName(), "sPhotoUri=" + Uri.parse("file:/"+ sPhotoFileName));
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+ sPhotoFileName));
    startActivity(Intent.createChooser(emailIntent, "Send mail..."));

从亚行日志猫:

V/DumbDumpersMain( 3972):   sPhotoUri=file://sdcard/DumbDumpers/DumbDumper.jpg
I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.CHOOSER comp={android/com.android.internal.app.ChooserActivity} (has extras) }
I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.SEND type=jpeg/image flags=0x3000000 comp={com.google.android.gm/com.google.android.gm.ComposeActivityGmail} (has extras) }
I/ActivityManager(   56):   Starting activity: Intent { action=android.intent.action.SEND type=jpeg/image flags=0x2800000 comp={com.google.android.gm/com.google.android.gm.ComposeActivity} (has extras) }
D/gmail-ls(  120):      MailProvider.query: content://gmail-ls/labels/me@gmail.com(null, null)
D/Gmail   ( 2507):      URI FOUND:file://sdcard/DumbDumpers/DumbDumper.jpg

看起来 email 提供程序正在附加一个长度为 0 的文件。 当我检查文件系统时,文件在那里并且正确。 在尝试 email 之前,创建图像文件的代码已经完成。

有人在没有魔法重启的情况下解决了这个问题(我已经尝试过了)?

问候,

更新

我的道路应该是

file:///sdcard/DumbDumpers/DumbDumper.jpg

你需要额外的/因为它指向根目录,即:

file:// + /sdcard/DumbDumpers/DumbDumper.jpg

结合为

file:///sdcard/DumbDumpers/DumbDumper.jpg

在上面的代码片段中,您需要:

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ sPhotoFileName));

我希望这有帮助。 我花了很长时间来调试。

问候,
芬莱

只是我这边的一点评论。 我在使用 GMail 时遇到了同样的问题,但不知何故,当我首先将有问题的文件存储在 SD 卡上并从那里而不是从资产中检索它时,它似乎可以工作。 所以我的代码如下:

Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, "Title");
i.putExtra(Intent.EXTRA_TEXT, "Content");
i.putExtra(Intent.EXTRA_STREAM, uri);
i.setType("text/plain");
startActivity(Intent.createChooser(i, "Send mail"));

和这里,

uri = Uri.fromFile(new File(context.getFilesDir(), FILENAME));

不起作用,而

uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), FILENAME));

做。

问候,迈克尔

而不是 "Uri.parse" 使用 "Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"file name"))"

Environment.getExternalStorageDirectory() - SD 卡或任何其他外部存储的路径

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"example@mail.com"});
    i.putExtra(Intent.EXTRA_SUBJECT, "Data from app");
    i.putExtra(Intent.EXTRA_TEXT   , "experience number x");
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "filename.txt"));
    i.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(i, "Send email..."));

看来这实际上是正确的,不确定发生了什么,但重新启动后它开始工作:/

我遇到了同样的问题,到处寻找解决方案。 最后,我通过找到一个开箱即用的开源应用程序解决了这个问题,并研究了他们是如何做到的。 代码比较长,所以我不会在这里引用它,而是发布一个链接。 查看第 449 行中的 sendEmail function

http://rehearsalassist.svn.sourceforge.net/viewvc/rehearsalassist/android/trunk/src/urbanstew/RehearsalAssistant/SessionPlayback.java?revision=94&view=markup

我将我的代码重构为相似,现在它可以工作了。 我希望这将帮助其他处于相同情况的人。

RFC 1738第 3.10 节:

文件 URL 采用以下形式:

   file://<host>/<path>

其中host是可以访问路径的系统的完全限定域名, path目录/目录/.../name形式的分层目录路径。

所以它是 file:///path/from/root 就像http://host/path/from/root因为在第二个和第三个斜杠之间有一个隐含的“localhost”。 但是如上所述,使用 Uri.FromFile() 来构建它。

我有同样的症状。 就我而言,这是因为我最初使用MODE_PRIVATE权限保存附件。 一旦我将其更改为MODE_WORLD_READABLE ,似乎 GMail 就能够访问该文件并正确发送附件。

看更多

这对我来说非常完美:在这个解决方案中,Nicolas 在 Cache 文件夹中创建了一个副本,并且 gmail 意图可以访问! http://stephendnicholas.com/archives/974

public void sendMail(String path) {
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
        new String[] {AppConstant.server_mail});
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
        "IBPS ERROR Mail");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
        "This is an autogenerated mail from IBPS app");
        emailIntent.setType("image/png");
        Uri myUri = Uri.parse("file://" + path);
        emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        }

也尝试添加 Intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 这有助于解决我的问题。

4天后我得到了解决方案,请注意以下几点,同时在Android(Java)中给出文件class的路径:

1) 内部存储使用路径 String path="/storage/sdcard0/myfile.txt";

2) path="/storage/sdcard1/myfile.txt";

3)在清单文件中提及权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

4) 首先检查文件长度以确认。

5) 检查 ES File Explorer 中关于 sdcard0 和 sdcard1 的路径是否相同,否则......

例如

File file=new File(path); 
long=file.length();//in Bytes

发送带有附件的 email:(通过文档)

意图 emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);

emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon@example.com"});

emailIntent.putExtra(Intent.EXTRA_SUBJECT, "电子邮件主题"); emailIntent.putExtra(Intent.EXTRA_TEXT, "电子邮件信息文本"); emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));

// 您也可以通过传递 Uris 的 ArrayList 来附加多个项目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM