Android 尝试将文件从 SD 卡附加到电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/587917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:19:15  来源:igfitidea点击:

Trying to attach a file from SD Card to email

androidemailattachmentandroid-intent

提问by Chrispix

I am trying to launch an Intent to send an email. All of that works, but when I try to actually send the email a couple 'weird' things happen.

我正在尝试启动 Intent 以发送电子邮件。所有这些都有效,但是当我尝试实际发送电子邮件时,会发生一些“奇怪”的事情。

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. No big deal. I hit send, and it sends. The only thing is the attachment does NOT get sent.

因此,如果我使用 Gmail 菜单上下文启动它会显示附件,让我输入电子邮件的收件人,并编辑正文和主题。没什么大不了。我点击发送,它发送。唯一的事情是附件没有被发送。

So. I figured, why not try it w/ the Email menu context (for my backup email account on my phone). 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? What am I doing wrong?

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

回答by Finlay

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[] 
    {"[email protected]"}); 
    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/[email protected](null, null)
D/Gmail   ( 2507):      URI FOUND:file://sdcard/DumbDumpers/DumbDumper.jpg

Looks like the email provider is attaching a 0 length file. 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.

看起来电子邮件提供商附加了一个长度为 0 的文件。当我检查文件系统时,文件在那里并且是正确的。创建图像文件的代码在尝试通过电子邮件发送之前已经完成。

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

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

you need the extra /as this points to the root directory, i.e.:

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

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

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

combined as

合并为

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

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

问候,
芬莱

回答by Michael F

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. So my code is the following:

只是我这边的一点意见。我在使用 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"));

and here,

和这里,

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

does notwork, whereas

没有工作,而

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

does.

做。

Regards, Michael

问候,迈克尔

回答by Snigdha

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() - SDcard 或任何其他外部存储的路径

回答by Chrispix

It appears that this is actually correct, not sure what was happening, but after a reboot it started working :/

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

回答by Eduardo Matos

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"[email protected]"});
    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..."));

回答by Railsdev

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 函数

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.

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

回答by android.weasel

From RFC 1738section 3.10:

来自RFC 1738第 3.10 节:

A file URL takes the form:

文件 URL 采用以下形式:

   file://<host>/<path>

where hostis the fully qualified domain name of the system on which the path is accessible, and pathis 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/rootbecause there's an implicit 'localhost' between the second and third slash. But as mentioned above, use Uri.FromFile() to build it.

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

回答by CasualCoder

I had the same symptoms. In my case it was because I was initially saving the attachment with the permissions MODE_PRIVATE. As soon as I changed it to MODE_WORLD_READABLEit seems GMail was then able to access the file and send the attachment properly.

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

See more

查看更多

回答by lucasddaniel

It's work perfectly for me: On this solution the Nicolas create one copy inside Cache folder and here gmail intent has access! http://stephendnicholas.com/archives/974

它对我来说非常有用:在这个解决方案中,尼古拉斯在 Cache 文件夹中创建了一个副本,在这里 gmail 意图可以访问! http://stephendnicholas.com/archives/974

回答by Jotiram Chavan

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