2013-01-01 45 views
1

对于我的其中一个项目,我试图简单地将图片附加到电子邮件并发送。为Android上传图片电子邮件附件的问题

  Intent emailIntent = new Intent(Intent.ACTION_SEND); 
     emailIntent.setType("image/jpg"); 
     emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
     emailIntent.putExtra(Intent.EXTRA_SUBJECT, 
     "Image attached."); 
     emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath)); 
     emailIntent.setType("text/plain"); 
     startActivity(Intent.createChooser(emailIntent, 
     "Send email using..")); 

我的变量“文件路径”是在我的设备的外部存储设备中找到的图像的绝对路径。它的形式是“/ mnt/sdcard/.....”我的图像路径绝对正确,因为我已成功将照片加载到其他图像视图中。

此意图也可以正常工作,并且能够将我带到屏幕上以选择用于发送图像的应用程序。但是,在实际的电子邮件中,我可以看到我的图像已附加(文件路径名是100%正确的),但图像本身未附加。

有没有人有一个想法,可能是什么原因导致这个问题?

+0

看这它可以帮助你解决你的问题。 http://stackoverflow.com/questions/2518055/image-attachment-to-a-mail-how-in-android –

回答

1

试试这个:

File fileToAttach = new File(filePath, filename); 
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileToAttach)); 
+0

简短而甜美。为我工作。我的问题是,我是从文件中获取字符串,并采取了'file://'而不是'file:///'。将它设置为'File'对象而不是'String'解决了所有问题。有趣的是,它在预览文件并将其发送到Evernote时起作用,但在试图通过Gmail应用程序发送电子邮件时破坏了。 – Muz

0

也越来越相同的问题

代码

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

从亚行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 

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

任何人修复这个没有魔术重启(我已经试过了)?

更新我

路径应该是

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

你需要额外的/因为这点到根目录下,即:

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

合并为

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

在上面的代码中,你需要:

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

我希望这有助于。我花了很多时间去调试。

+0

嘿Dixit,谢谢。我尝试了你的方法,并且我的最终路径与上面列出的文件路径格式完全相同。但图像仍然没有得到附加,但文件肯定是存在的,路径绝对正确:( –

0

尝试这个办法:

File pngDir = new File(Environment.getExternalStorageDirectory(),"DCIM/Camera"); 
File pngfile = new File(pngDir, "<ImageName>"); 
Uri pngUri = Uri.fromFile(pngfile); 
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("image/*"); 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,"<Email>"); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"<Subject>"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"<Message>"); 
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri); 
emailIntent.setType("image/png"); 
startActivity(Intent.createChooser(emailIntent, "Send mail...")); 
0

试试这个代码

  Intent sendIntent = new Intent(Intent.ACTION_SEND); 
      sendIntent.setType("message/rfc822"); 
      sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Video"); 
      sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+path)); 
      sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the Video"); 
      startActivity(Intent.createChooser(sendIntent, "Email:"))