Android无法通过电子邮件发送附件(gmail和其他电子邮件客户端)

时间:2017-09-25 14:32:35

标签: android email gmail

我无法在相关问题中找到帮助......所以: 我尝试使用以下代码发送带附件的电子邮件:

String pdfName = "filename.pdf";
    File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), pdfName);
    Uri path = Uri.fromFile(filelocation);
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setType("application/pdf");
    String to[] = {"email@email.com"};
    emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
    emailIntent.putExtra(Intent.EXTRA_STREAM, path);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    startActivity(Intent.createChooser(emailIntent, "Send email..."));

一切正常,我可以选择一个电子邮件客户端,电子邮件客户端会打开一封包含所有内容的新电子邮件。但是在我发送电子邮件后,我发现错误并且无法发送attachement" (Emailclient = Gmail)。与GMX相同的是电子邮件客户端("不支持此附件")。 请帮帮我。

修改

我解决了我的问题。 pdf文件的路径不正确。我还包括一个文件选择器。这是我的解决方案:

private static final int PICK_FROM_GALLERY = 101;
static Uri URI = null;

@Override
public boolean onOptionsItemSelected(MenuItem item) {  
    if(item.getItemId() == R.id.sendEmail){
        openGallery();
    }

    return true;
}

public void openGallery(){
    Intent intent = new Intent();
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.putExtra("return-data", true);
    startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK){
        String fname = data.getDataString();
        URI = Uri.parse(fname);

        sendMail();
    }
}

private void sendMail(){
    if(URI != null) {
        try {
            final Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"janssen.mark@gmx.de"});
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Database");
            if (URI != null) {
                emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
            }
            emailIntent.putExtra(Intent.EXTRA_TEXT, "Im Anhang befindet sich die Datenbank als PDF-Datei.");
            this.startActivity(Intent.createChooser(emailIntent, "Sending email..."));
        } catch (Throwable throwable) {
            Toast.makeText(this, "Request failed: " + throwable.toString(), Toast.LENGTH_LONG).show();
        }
    }
}

0 个答案:

没有答案
相关问题