Trying to attach a file from SD Card to email

尝试通过Intent发送带有JPEG图片附件的电子邮件时遇到问题。在某些应用中,附件未被发送;而在另一些应用中,虽然附件可以正确发送,但邮件的主题和正文却丢失了。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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.

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.

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?

 

 

Also getting the same problem

Code:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
    emailIntent
.setType("jpeg/image");
    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..."));


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.

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 / for the root

so it's like

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

combined is

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

So in my example you need:

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

hope this helps. Took me ages to debug.

Regards,

Finlay


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值