【问题标题】:Android email intent not attaching files as attachmentAndroid电子邮件意图未将文件附加为附件
【发布时间】:2017-11-28 17:19:02
【问题描述】:

对于我的公司,我正在尝试使用电子邮件意图从我的 android 应用程序发送电子邮件。 我正在使用模拟器来测试我的应用程序。但问题是当我尝试添加和附件(例如 pdf、图像)时,它还不会附加。 这是我的代码:

      private String SendEmail(String oid, final String img, final String party_code, final String order_by, final Bitmap attachimg, final String note) {
        try {
       String filename="DOAttachment.jpeg";
            String subject ="Order "+oid+" has been sent successfully";
            String body="\nDear Sir, \n"+"Please find the attached file herewith.\nThe D.O for the customer(party) "+party_code+" has been successfully done with the order number: "+oid+"\n\n\n"+"With regards \n \n Employee code/ID: "+order_by+"\n\nN.B:"+note;

 File root = Environment.getExternalStorageDirectory();
            String pathToMyAttachedFile = "DirName/"+filename;
            File file = new File(root, pathToMyAttachedFile);
            file.setReadable(true,false);
            Log.e("File path"," "+file);    
            //using outlook    
            Intent intent = new Intent(Intent.ACTION_VIEW).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);
            intent.setType("image/*");
            Uri data = Uri.parse("mailto:?subject=" + subject + "&body=" + body+ "&stream="+Uri.parse("file:///"+Environment.getExternalStorageDirectory().getAbsolutePath())+"/DirName/"+filename);
            intent.setData(data);
            intent .putExtra(Intent.EXTRA_EMAIL, toemail);
            if (!file.exists() || !file.canRead()||!file.canWrite()) {
                Log.e(" FILE ERROR ","File Not found");
                Toast.makeText(getApplicationContext(),"File Not found",Toast.LENGTH_LONG).show();
            }
            else {
                file.setReadable(true);
                Log.e(" FILE OK ","File was found");    
                Uri uri = Uri.fromFile(file);    
                intent.putExtra(Intent.EXTRA_STREAM, uri);
            }
 ));
 if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }    
     return "TRUE";    

        } catch (Exception e) {
            Log.e("SendMail", e.getMessage(), e);
            return "MAIL NOT SENT";
        }
    }    

结果是带有空附件的电子邮件查找截图:https://1drv.ms/i/s!AruisQQIx8MTgatuhJFmSaoArg_6Xw

【问题讨论】:

标签: android email


【解决方案1】:

检查您是否在清单和运行时为您的应用程序提供了 READ_EXTERNAL_STORAGE 权限。

然后调用以下方法发送邮件,假设完整的文件路径保存在 filePath 中。

  File root = Environment.getExternalStorageDirectory();
  String pathToMyAttachedFile = "DirName/"+filename;
  File filePath = new File(root, pathToMyAttachedFile)
  sendEmailAlert(filePath,subject,text);

调用方法

 private void sendEmailAlert(File fileName,String subject,String text) {

    final File file=fileName;

                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("application/octet-stream"); /* or use intent.setType("message/rfc822); */
                    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
                    intent.putExtra(Intent.EXTRA_TEXT, text);
                    if (!file.exists() || !file.canRead()) {
                        Toast.makeText(getContext(), "Attachment Error", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Uri uri = Uri.fromFile(file);
                    intent.putExtra(Intent.EXTRA_STREAM, uri);
                    startActivity(Intent.createChooser(intent, "Send email..."));

}

【讨论】:

  • 是的,我会在清单和登录期间提供完全权限(启动活动) 并且还使用 ActivityCompat.requestPermissions(LoginActivity.this, new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE }, 1); .无论如何感谢您的回复。
  • 你试过我的代码了吗?此代码上周在我的项目中成功实施
  • 我也在使用相机意图拍摄附件图像所以我认为没关系。因为我在目录中获取图像文件。此外,我正在尝试仅使用选定的电子邮件客户端,因此我忽略了意图选择器
  • 是的,它的工作,但如果我只想选择 gmail 或 Outlook 那我该怎么办?
  • 我在代码中添加了更改。核实。使用intent.setType()
猜你喜欢
  • 2017-04-11
  • 2015-03-06
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 2015-10-23
相关资源
最近更新 更多