安卓:问题附件从我的应用程序发送电子邮件 [英] Android: problem sending email with attachment from my application

查看:103
本文介绍了安卓:问题附件从我的应用程序发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序允许用户创建和修改文件。我希望他们能够发送文件作为电子邮件附件。所以,我需要首先创建并写入到一个临时文件,然后我附加到电子邮件。然后我想删除临时文件的电子邮件程序完成时。遗憾的是,Gmail应用程序与结果code尽快作出响应,用户点击发送;如果我只要结果code接收到删除的文件,没有附件发送。

它可能是别的东西是怎么了?和附件不发送出于不同的原因,但我pretty肯定我的判断是正确的,因为低于code正常工作,如果我注释掉 mEmailTmpFile.delete()电话。它也工作正常,如果我做了什么非常不可取的,比如视频下载(4000)前夕 mEmailTmpFile.delete()

反正当电子邮件被发送完成得到通知?或者我应该怎么解决这个任何其他建议?

  //发送电子邮件...文件externalStorage = Environment.getExternalStorageDirectory();
串sdcardPath = externalStorage.getAbsolutePath();
mEmailTmpFile =新的文件(sdcardPath +/+姓名);//做一些其他的,以保证unqiueness,然后写入文件...//全部完成编写,发送电子邮件意图sendIntent =新意图(Intent.ACTION_SEND);
sendIntent.setType(应用程序/压缩);
sendIntent.putExtra(Intent.EXTRA_SUBJECT,名);
sendIntent.putExtra(Intent.EXTRA_TEXT,文件附后。);
sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(文件://+ mEmailTmpFile.getPath()));
startActivityForResult(Intent.createChooser(sendIntent,电子邮件),请求code_EMAIL);公共同步无效的onActivityResult(INT REQ code,INT结果code,意图数据)
{
   如果(REQ code == REQUEST code_EMAIL)
   {
    mEmailTmpFile.delete();
    }
}


解决方案

在我的应用程序,我不删除临时文件。 Android将通过删除该文件,如果它需要的空间照顾它。我会确保你没有创建在SD卡根目录下的tmp文件,因为这可能显得凌乱但除此之外,不应该是一个问题。

My application allows users to create and modify files. I would like them to be able to send a file as an email attachment. So, I need to first create and write to a temporary file, which I then attach to the email. And then I would like to delete the temporary file when the email program finishes. Unfortunately, the gmail app responds with a result code as soon as the user clicks "send"; and if I delete the file as soon as the result code is received, no attachment is sent.

Its possible that something else is going wrong and the attachment is not sent for a different reason, but I'm pretty sure my assessment is correct because the below code works properly if I comment out the mEmailTmpFile.delete() call. It also works fine if I do something very undesirable like Thread.sleep(4000) immediately prior to mEmailTmpFile.delete().

Is there anyway to be notified when the email is done sending? Or any other suggestions for how I should work around this?

//send an email...

File externalStorage = Environment.getExternalStorageDirectory();
String sdcardPath = externalStorage.getAbsolutePath();
mEmailTmpFile = new File(sdcardPath + "/" +  name );

//do some other to ensure unqiueness and then write to the file...

//all done writing, send email

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("application/zip");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, name);
sendIntent.putExtra(Intent.EXTRA_TEXT, "File attached.");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ mEmailTmpFile.getPath()));
startActivityForResult(Intent.createChooser(sendIntent, "Email"), REQUESTCODE_EMAIL);

public synchronized void onActivityResult(int reqCode, int resultCode, Intent data)
{
   if (reqCode == REQUESTCODE_EMAIL)
   {
    mEmailTmpFile.delete();
    }
}

解决方案

In my apps, I don't delete the temporary file. Android will take care of it by deleting the file if it needs space. I would ensure that you don't create the tmp file in the SDCard root directory since that can look messy but other than that there shouldn't be a problem.

这篇关于安卓:问题附件从我的应用程序发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆