【问题标题】:AndroidStudio attaching PDF to email issueAndroid Studio 将 PDF 附加到电子邮件问题
【发布时间】:2015-11-04 04:56:04
【问题描述】:

谁能帮助解释为什么此代码在 Geneymotion 模拟器中有效,但在任何实际的 android 设备中无效?该代码在模拟器中完美运行,我没有遇到任何问题,我完全不明白为什么它不能在实际设备中运行。

下面是点击“创建PDF”按钮时调用的方法:

    try {

        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/invoices";

        File dir = new File(path);
        if(!dir.exists())
            dir.mkdirs();

        Log.d("PDFCreator", "PDF Path: " + path);

        File delete = new File(dir, "invoice.pdf");
        if (delete.exists())
            delete.delete();

        File file = new File(dir, "invoice.pdf");
        FileOutputStream fOut = new FileOutputStream(file);

        PdfWriter.getInstance(doc, fOut);

        //open the document
        doc.open();
        Log.d("test", "PDF Doccument Opened for input");

在模拟器中: - 当我单击“创建 PDF”按钮时,会调用电子邮件意图,我可以在电子邮件客户端之间进行选择以使用并通过它发送 PDF。

在实际的安卓设备中: 当我在实际设备中单击“创建 PDF”时,绝对没有任何反应。

我认为问题可能与我存储 PDF 的目录有关,但我无法解决它。

如果有人可以帮助我,将不胜感激。提前致谢。

注意:我正在使用 DroidText 库来生成 PDF。

在实际设备上的测试日志显示:

11-05 21:09:14.839  10375-10375/motawaze.com.invoicepdf D/dalvikvm﹕ GC_EXTERNAL_ALLOC freed 93K, 47% free 2913K/5447K, external 0K/0K, paused 30ms
11-05 21:09:25.989  10375-10375/motawaze.com.invoicepdf D/CLIPBOARD﹕ Hide Clipboard dialog at Starting input: finished by someone else... !
11-05 21:09:29.849  10375-10378/motawaze.com.invoicepdf D/dalvikvm﹕ GC_CONCURRENT freed 153K, 47% free 3043K/5639K, external 171K/1281K, paused 7ms+2ms
11-05 21:09:39.899  10375-10378/motawaze.com.invoicepdf D/dalvikvm﹕ GC_CONCURRENT freed 351K, 48% free 3105K/5895K, external 171K/1281K, paused 7ms+3ms
11-05 21:09:53.639  10375-10375/motawaze.com.invoicepdf D/test﹕ set the fields for PDF input
11-05 21:09:53.649  10375-10375/motawaze.com.invoicepdf D/test﹕ set the document
11-05 21:09:53.669  10375-10375/motawaze.com.invoicepdf D/test﹕ set the calender
11-05 21:09:53.669  10375-10375/motawaze.com.invoicepdf D/PDFCreator﹕ PDF Path: /mnt/sdcard/Download/invoices
11-05 21:09:53.669  10375-10375/motawaze.com.invoicepdf E/PDFCreator﹕ ioException:java.io.FileNotFoundException: /mnt/sdcard/Download/invoices/Invoice.pdf (No such file or directory)
11-05 21:10:31.420  10375-10378/motawaze.com.invoicepdf D/dalvikvm﹕ GC_CONCURRENT freed 358K, 48% free 3152K/5959K, external 171K/1281K, paused 7ms+2ms

【问题讨论】:

  • 请在此处发布您的日志。
  • 如果代码在模拟器中没有问题,我看不到如何发布日志?
  • 我只是想看看你在这一行得到了什么:Log.d("PDFCreator", "PDF Path: " + path);
  • 11-04 05:36:50.240 2211-2211/motawaze.com.invoicepdf D/PDFCreator:PDF 路径:/mnt/sdcard/Download/invoices
  • 顺便说一下,日志来自android studio,我不知道如何从实际的android设计中获取日志,如果有办法,请告诉我,我会尝试提取它。

标签: android pdf android-intent android-droidtext


【解决方案1】:

正如聊天中提到的那样。看起来您的 PDF 没有被创建,因此您得到了FileNotFoundException。查看您的 PDF 创建代码。这就是问题所在。

更改这些行:

 if(!dir.exists())
     dir.mkdirs();

到:

 boolean created = false;
 if(!dir.exists())
     created = dir.mkdirs();

 if(created)
  Log.d("test", "Path created");

这将检查目录是否已创建。

【讨论】:

  • 如果您能解释一下,在我的场景中,什么可能导致应用程序在模拟器中工作,但在实际设备中抛出 FileNotFoundException,请提供更多详细信息以提供帮助?如果你这样做,我会选择你的答案是正确的。
  • 把设备的整个日志放在这里。
  • 我正在 Gingerbread android 设备上测试它。我假设它与访问目录有关,但我已经在 android 清单文件中设置了它:''。这被放置在 ''
  • 我认为,我在您的代码中看到的另一个错误是,此日志:11-05 21:09:53.669 10375-10375/motawaze.com.invoicepdf E/PDFCreator﹕ioException:java.io .FileNotFoundException: /mnt/sdcard/Download/invoices/Invoice.pdf (没有这样的文件或目录)这一行有大写“I”的发票,在你的代码中,发票是小“i”
  • 这很奇怪,到底是怎么做到的?我很困惑,我在你的答案中使用了代码并且没有抛出异常,你能解释一下为什么吗?我知道它会检查目录是否已创建,但检查有何不同?
【解决方案2】:

此代码可能会对您有所帮助。

我使用 DroidText.0.2.jar 创建 Pdf。

public void createPDF()
    {
        Document doc = new Document();

        try {
            path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText";
//            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

            File dir = new File(path);
            if(!dir.exists())
                dir.mkdirs();

            Log.d("PDFCreator", "PDF Path: " + path);


            file = new File(path, "HomeInventory.pdf");
            FileOutputStream fOut = new FileOutputStream(file);

            PdfWriter.getInstance(doc, fOut);

            //open the document
            doc.open();


            Paragraph p1 = new Paragraph("Hi! I am generating my first PDF using DroidText");
            Font paraFont= new Font(Font.COURIER);
            p1.setAlignment(Paragraph.ALIGN_CENTER);
            p1.setFont(paraFont);

            //add paragraph to document
            doc.add(p1);

            Paragraph p2 = new Paragraph("This is an example of a paragraph");
            Font paraFont2= new Font(Font.COURIER,14.0f, Color.GREEN);
            p2.setAlignment(Paragraph.ALIGN_CENTER);
            p2.setFont(paraFont2);

            doc.add(p2);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
//            Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.android);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.action_search);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
            Image myImg = Image.getInstance(stream.toByteArray());
            myImg.setAlignment(Image.MIDDLE);
            //add image to document
            doc.add(myImg);

            //set footer
            Phrase footerText = new Phrase("This is an example of a footer");
            HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
            doc.setFooter(pdfFooter);



        } catch (DocumentException de) {
            Log.e("PDFCreator", "DocumentException:" + de);
        } catch (IOException e) {
            Log.e("PDFCreator", "ioException:" + e);
        }
        finally
        {
            doc.close();
        }
    }

以这种方式将此创建的 Pdf 文件附加到电子邮件中..

String[] mailto = {"me@gmail.com"};
                Uri uri = Uri.fromFile(file);

                Intent emailIntent = new Intent(Intent.ACTION_SEND,Uri.parse("mailto:"));
                emailIntent.setType("text/plain");
                emailIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My Subject");
                emailIntent.putExtra(Intent.EXTRA_TEXT, "My Body");

                emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

                emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
                getActivity().startActivity(emailIntent);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多