Android电子邮件意图未将文件作为附件附加 [英] Android email intent not attaching files as attachment

查看:71
本文介绍了Android电子邮件意图未将文件作为附件附加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的公司,我正尝试使用电子邮件意图从我的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

The result is email with empty attachment find screenshot : https://1drv.ms/i/s!AruisQQIx8MTgatuhJFmSaoArg_6Xw

推荐答案

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

Check whether you had provided READ_EXTERNAL_STORAGE permission for your application both in manifest and run-time.

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

Then Call the below method send mail assuming the full file path is saved in 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..."));

}

这篇关于Android电子邮件意图未将文件作为附件附加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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