简体   繁体   English

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

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

I am trying to launch an Intent to send an email.我正在尝试启动发送 email 的意图。 All of that works, but when I try to actually send the email a couple 'weird' things happen.所有这些都有效,但是当我尝试实际发送 email 时,会发生一些“奇怪”的事情。

here is code这是代码

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

So if I launch using the Gmail menu context It shows the attachment, lets me type who the email is to, and edit the body & subject.因此,如果我使用 Gmail 菜单上下文启动它会显示附件,让我输入 email 是谁,然后编辑正文和主题。 No big deal.没什么大不了。 I hit send, and it sends.我点击发送,它发送。 The only thing is the attachment does NOT get sent.唯一的问题是附件没有发送。

So.所以。 I figured, why not try it w/ the Email menu context (for my backup email account on my phone).我想,为什么不尝试使用 Email 菜单上下文(用于我手机上的备份 email 帐户)。 It shows the attachment, but no text at all in the body or subject.它显示附件,但正文或主题中根本没有文本。 When I send it, the attachment sends correctly.当我发送它时,附件会正确发送。 That would lead me to believe something is quite wrong.那会让我相信有些事情是完全错误的。 Do I need a new permission in the Manifest launch an intent to send email w/ attachment?我是否需要在 Manifest 启动时获得新许可才能发送带有附件的 email? What am I doing wrong?我究竟做错了什么?

Also getting the same problem也遇到同样的问题

Code:代码:

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

From adb logcat:从亚行日志猫:

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

Looks like the email provider is attaching a 0 length file.看起来 email 提供程序正在附加一个长度为 0 的文件。 When I check the filesystem the file is there and correct.当我检查文件系统时,文件在那里并且正确。 The code which creates the image file is well finished prior to the attempt to email it.在尝试 email 之前,创建图像文件的代码已经完成。

Anyone fixed this without magic reboots (I've already tried that)?有人在没有魔法重启的情况下解决了这个问题(我已经尝试过了)?

Regards,问候,
Fin

Update更新

Path for me should have been我的道路应该是

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

you need the extra / as this points to the root directory, ie:你需要额外的/因为它指向根目录,即:

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

combined as结合为

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

In the above snippet you need:在上面的代码片段中,您需要:

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

I hope this helps.我希望这有帮助。 It took me ages to debug.我花了很长时间来调试。

Regards,问候,
Finlay芬莱

Just a little remark from my side.只是我这边的一点评论。 I've been having the same issues with GMail, but somehow it seems to work when I store the file in question on the SD card first and retrieve it from there, rather than from the assets.我在使用 GMail 时遇到了同样的问题,但不知何故,当我首先将有问题的文件存储在 SD 卡上并从那里而不是从资产中检索它时,它似乎可以工作。 So my code is the following:所以我的代码如下:

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

and here,和这里,

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

does not work, whereas不起作用,而

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

does.做。

Regards, Michael问候,迈克尔

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

Environment.getExternalStorageDirectory() - path to SDcard or any other external storage 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..."));

It appears that this is actually correct, not sure what was happening, but after a reboot it started working:/看来这实际上是正确的,不确定发生了什么,但重新启动后它开始工作:/

I got the same problem and looked everywhere for a solution.我遇到了同样的问题,到处寻找解决方案。 Finally I solved it by finding an open source app that worked out of the box and looked at how they did it.最后,我通过找到一个开箱即用的开源应用程序解决了这个问题,并研究了他们是如何做到的。 The code is rather long so I won't quote it here but post a link.代码比较长,所以我不会在这里引用它,而是发布一个链接。 Look at the sendEmail function in line 449查看第 449 行中的 sendEmail function

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

I refactored my code to be similar, and now it works.我将我的代码重构为相似,现在它可以工作了。 I hope this will help others in the same situation.我希望这将帮助其他处于相同情况的人。

From RFC 1738 section 3.10:RFC 1738第 3.10 节:

A file URL takes the form:文件 URL 采用以下形式:

   file://<host>/<path>

where host is the fully qualified domain name of the system on which the path is accessible, and path is a hierarchical directory path of the form directory/directory/.../name .其中host是可以访问路径的系统的完全限定域名, path目录/目录/.../name形式的分层目录路径。

So it's file:///path/from/root just like http://host/path/from/root because there's an implicit 'localhost' between the second and third slash.所以它是 file:///path/from/root 就像http://host/path/from/root因为在第二个和第三个斜杠之间有一个隐含的“localhost”。 But as mentioned above, use Uri.FromFile() to build it.但是如上所述,使用 Uri.FromFile() 来构建它。

I had the same symptoms.我有同样的症状。 In my case it was because I was initially saving the attachment with the permissions MODE_PRIVATE .就我而言,这是因为我最初使用MODE_PRIVATE权限保存附件。 As soon as I changed it to MODE_WORLD_READABLE it seems GMail was then able to access the file and send the attachment properly.一旦我将其更改为MODE_WORLD_READABLE ,似乎 GMail 就能够访问该文件并正确发送附件。

See more 看更多

It's work perfectly for me: On this solution the Nicolas create one copy inside Cache folder and here gmail intent has access!这对我来说非常完美:在这个解决方案中,Nicolas 在 Cache 文件夹中创建了一个副本,并且 gmail 意图可以访问! http://stephendnicholas.com/archives/974 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..."));
        }

Also try adding Intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);也尝试添加 Intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); This helped with my issue.这有助于解决我的问题。

I have got solution on this after 4 days, Please note following points while giving path to File class in Android(Java): 4天后我得到了解决方案,请注意以下几点,同时在Android(Java)中给出文件class的路径:

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

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

3) mention permissions in Manifest file. 3)在清单文件中提及权限。

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

4) First check file length for confirmation. 4) 首先检查文件长度以确认。

5) Check paths in ES File Explorer regarding sdcard0 & sdcard1 is this same or else...... 5) 检查 ES File Explorer 中关于 sdcard0 和 sdcard1 的路径是否相同,否则......

eg例如

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

Send an email with an attachment: (By docs)发送带有附件的 email:(通过文档)

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

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

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

// You can also attach multiple items by passing an ArrayList of Uris // 您也可以通过传递 Uris 的 ArrayList 来附加多个项目

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

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