【问题标题】:Gmail wont send email attachment pushed from appGmail 不会发送从应用推送的电子邮件附件
【发布时间】:2015-11-06 19:27:11
【问题描述】:

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

EDIT通过添加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();
    }

【问题讨论】:

  • 哪个邮件客户端发送失败?
  • Gmail 和谷歌浏览器也无法保存
  • 可能是文件位置被拒绝。 This question 有类似的问题,通过将文件转移到公共区域解决了

标签: java android email pdf android-studio


【解决方案1】:

这是权限问题还是什么?

是的。除了您的应用程序之外,地球上只有零个应用程序可以访问您的文件,因为它位于 internal storage 上,对您的应用程序来说是私有的。

另外,绝不使用硬编码路径。您的 /data/data/ 内容将在用户使用辅助帐户的每台 Android 4.2+ 设备上失败。

有什么建议吗?

您可以坚持使用内部存储,但您将需要使用a FileProvider,也许使用my LegacyCompatCursorWrapper 来提供文件。 This sample app 演示了这一点,尽管使用 ACTION_VIEW 而不是 ACTION_SEND

或者,将文件放在external storage

【讨论】:

  • 通过添加到外部,这样如果应用程序被删除,文件仍会保留在设备上,对吗?
  • @JJStamp:不一定。如果您使用getExternalFilesDir(),该目录中的文件将在卸载时被删除,就像getFilesDir() 对内部存储所做的那样。如果,OTOH,你在别处写(例如,Environment.getExternalStoragePublicDirectory()),这些文件将在卸载后继续存在。
  • 编辑的答案与保存的 PDF 到外部缓存,问题仍然存在
  • @JJStamp:首先,永远不要使用连接来构建路径。使用new File(getExternalCacheDir(), PDFname)。其次,PDF 的 MIME 类型是 application/pdf,而不是 message/rfc822。否则,这应该没问题,虽然我从未尝试过getExternalCacheDir(),只有getExternalFilesDir()
【解决方案2】:

除了权限之外,我还遇到了一个问题。我猜这与 Gmail 懒惰地拿着 Uri 但直到(有时几分钟)之后才阅读它有关。您有时会注意到邮件位于发件箱中。

似乎如果您在电子邮件退出发件箱之前删除文件,则电子邮件会在不带附件的情况下发送。我不得不在删除文件之前等待多长时间添加一个很长的延迟

【讨论】:

    猜你喜欢
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 2016-07-19
    • 1970-01-01
    • 2017-02-09
    • 2016-09-22
    • 2013-10-17
    相关资源
    最近更新 更多