Gmail不会发送从应用推送的电子邮件附件

时间:2015-08-13 17:29:55

标签: java android email pdf android-studio

在我的Android应用程序中,我创建了一个PDF,我希望用户能够通过电子邮件客户端从我的应用程序发送。现在,电子邮件客户端将显示PDF已附加但无法发送附件。这是权限问题还是什么?有什么建议吗?

编辑通过添加getExternalCacheDir()存储到外部并且问题仍然存在

电子邮件功能

private void EmailDialog(){
        //create a dialog that prompts the user whether or not they want to send the PDF
        dialogBuilder = new AlertDialog.Builder(this);
        final TextView txtInput = new TextView(this);

        dialogBuilder.setTitle("Email Audit");
        dialogBuilder.setMessage("Do you want to email this audit?");
        dialogBuilder.setView(txtInput);
        dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
            //allows user to send PDF over their email application choice
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("message/rfc822");
                PDF = new File(getExternalCacheDir() + PDFname);
                i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(PDF));
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(OldLocation.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                RemoveDialog();
            }
        });

        AlertDialog emailDialog = dialogBuilder.create();
        emailDialog.show();
    }

PDF功能

@TargetApi(Build.VERSION_CODES.KITKAT)
    private void createPDF(){


        // open a new document
        PrintAttributes pa = new PrintAttributes.Builder().setMediaSize(PrintAttributes.MediaSize.NA_LETTER).setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();
        PrintedPdfDocument document = new PrintedPdfDocument(this, pa);

        // start a page
        PdfDocument.Page page = document.startPage(0);

        // draw something on the page
        View content = textView;
        content.draw(page.getCanvas());



        // finish the page
        document.finishPage(page);

        // add more pages

        // write the document content

        try {
            File PDFs = getExternalCacheDir();
            File file = new File(PDFs, companyInfo.getName() + ".pdf");
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            document.writeTo(fos);
            fos.close();

            //close the document
            document.close();
        } catch (Exception ex){

        }

        realm.beginTransaction();
        companyInfo = realm.where(CompanyInfo.class).findFirst();
        companyInfo.removeFromRealm();
        realm.commitTransaction();
    }

2 个答案:

答案 0 :(得分:1)

  

这是权限问题还是什么?

是。除了你的文件之外,地球上正好有零个可以访问你文件的应用程序,就像internal storage一样,属于你的应用程序。

此外,永远没有硬编码路径。在用户位于辅助帐户的每台Android 4.2+设备上,您的/data/data/内容都会失败。

  

有什么建议吗?

您可以坚持使用内部存储,但您需要使用a FileProvider(可能还有my LegacyCompatCursorWrapper)来提供文件。 This sample app虽然使用了ACTION_VIEW而不是ACTION_SEND来证明了这一点。

或者,将文件放在external storage上。

答案 1 :(得分:0)

除了权限外,我还遇到了一个问题。我猜想这与Gmail懒惰持有Uri有关,但是直到(有时很多分钟)才阅读。您会发现有时邮件位于发件箱中。

如果您在电子邮件退出发件箱之前删除文件,则电子邮件将被发送而没有附件。我不得不在删除文件之前等待很长的时间

相关问题