简体   繁体   English

创建pdf文件后错误活动关闭

[英]error activity close after creating pdf file

i am trying to generate pdf .I successfully generate pdf also but my application is close after creating pdf .I search but not find any answer . 我正在尝试生成pdf。我也成功生成了pdf,但是创建pdf后我的应用程序已关闭。我搜索但未找到任何答案。 I follow this link Creating a pdf file in android programmatically and writing in it I dont want to close my application and view pdf in pdfviewer and this my code 我按此链接以编程方式在android中创建一个pdf文件并编写该文件,但我不想关闭我的应用程序并在pdfviewer中查看pdf以及此代码

    private void createPDF(ArrayList<Searchresult> alllist)  {

            com.itextpdf.text.Document doc= new com.itextpdf.text.Document(PageSize.A4,50,50,50,50);
            try {
                String USER_PASS = "hasu123";
                String OWNER_PASS = "vnr123";
                 String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir";

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

            File file = new File(dir, "newFile.pdf");
                FileOutputStream fOut = new FileOutputStream(file);
                PdfWriter writer=PdfWriter.getInstance(doc, fOut);

              /*  writer.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
                        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);*/
                HeaderAndFooter event = new HeaderAndFooter();
                writer.setPageEvent(event);

                //open the document
                doc.open();
               /* PdfReader reader = new PdfReader(path);
                PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(path));
                stamper.setEncryption(USER_PASS.getBytes(),USER_PASS.getBytes(),
                        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
                stamper.close();
                reader.close();*/

                PdfPTable table = new PdfPTable(8);
                Font subtitleFont = FontFactory.getFont("Times Roman",11, BaseColor.BLUE);
                Font subtitle = FontFactory.getFont("Times Roman",7, BaseColor.BLACK);

                PdfPCell pcell = new PdfPCell(new Phrase("Stoneid",subtitleFont));
                pcell.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell);
                PdfPCell pcell2 = new PdfPCell(new Phrase("Shape",subtitleFont));
                pcell2.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell2);
                PdfPCell pcell3 = new PdfPCell(new Phrase("Carat",subtitleFont));
                pcell3.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell3);
                PdfPCell pcell4 = new PdfPCell(new Phrase("Color",subtitleFont));
                pcell4.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell4);
                PdfPCell pcell5 = new PdfPCell(new Phrase("Clarity",subtitleFont));
                pcell5.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell5);
                PdfPCell pcell6 = new PdfPCell(new Phrase("Cut",subtitleFont));
                pcell6.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell6);
                PdfPCell pcell7 = new PdfPCell(new Phrase("Polish",subtitleFont));
                pcell7.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell7);
                PdfPCell pcell8 = new PdfPCell(new Phrase("Symm",subtitleFont));
                pcell8.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
                table.addCell(pcell8);
                table.setHeaderRows(1);

                for (int i=0;i<alllist.size();i++){
                    String stoneid=alllist.get(i).getStoneid();
                    String shape=alllist.get(i).getShape();
                    String carat=alllist.get(i).getSize();
                    String color=alllist.get(i).getColor();
                    String cut=alllist.get(i).getCut();
                    String polish=alllist.get(i).getPolish();
                    String symmm=alllist.get(i).getSym();
                    String clarity=alllist.get(i).getClarity();
                    if(color.equalsIgnoreCase("")){
                        color="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(stoneid.equalsIgnoreCase("")){
                        stoneid="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(shape.equalsIgnoreCase("")){
                        shape="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(carat.equalsIgnoreCase("")){
                        carat="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(color.equalsIgnoreCase("")){
                        color="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(cut.equalsIgnoreCase("")){
                        cut="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(symmm.equalsIgnoreCase("")){
                        symmm="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(polish.equalsIgnoreCase("")){
                        polish="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    if(clarity.equalsIgnoreCase("")){
                        clarity="*"; // this fills the cell with * if the String is empty otherwise cell won't be created
                    }
                    table.addCell(createCel(stoneid,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(shape,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(carat,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(color,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(cut,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(symmm,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(polish,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                    table.addCell(createCel(clarity,subtitle,com.itextpdf.text.Element.ALIGN_CENTER));
                }
                doc.add(table);
            } catch (DocumentException de) {
                Log.e("PDFCreator", "DocumentException:" + de);
            } catch (IOException e) {
                Log.e("PDFCreator", "ioException:" + e);
            }
            finally {
                doc.close();
            }
           viewPdf("newFile.pdf", "Dir");
        }


 private PdfPCell createCel(String s, Font subtitle, int alignCenter) {
        PdfPCell pdfPCel = new PdfPCell(new Phrase(s,subtitle));
        pdfPCel.setHorizontalAlignment(alignCenter);
        return pdfPCel;
    }


    private void viewPdf(String s, final String dir) {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                }
                runOnUiThread(new Runnable() {
                    public void run() {
                        File file = new File(Environment.getExternalStorageDirectory() + "/" + dir + "/" + "newFile.pdf");
                        Intent target = new Intent(Intent.ACTION_VIEW);
                        //target.setPackage("com.adobe.reader");
                        target.setDataAndType(Uri.fromFile(file),"application/pdf");
                        target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        Intent intent = Intent.createChooser(target, "Open File");

                        try {
                            startActivity(intent);
                        } catch (ActivityNotFoundException e) {
                            // Instruct the user to install a PDF reader here, or something
                            Toast.makeText(ResultDataNew.this, "No PDF Viewer is install", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }.start();
    }

this logcat: 此日志:

06-09 07:21:37.993 1334-1334/? E/libprocessgroup: failed to make and chown /acct/uid_10011: Read-only file system
06-09 07:21:37.993 1334-1334/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
06-09 07:21:37.993 1334-1334/? I/art: Late-enabling -Xcheck:jni
06-09 07:21:38.058 1334-1334/Abc.com W/System: ClassLoader referenced unknown path: /data/app/Abc.com-1/lib/x86
06-09 07:21:38.231 1334-1353/Abc.com D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                              [ 06-09 07:21:38.236  1334: 1334 D/         ]
                                                              HostConnection::get() New Host Connection established 0xe9b1eb90, tid 1334
06-09 07:21:38.296 1334-1353/Abc.com D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
06-09 07:21:38.297 1334-1353/Abc.com D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-09 07:21:38.310 1334-1353/Abc.com D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so

                                                      [ 06-09 07:21:38.329  1334: 1353 D/         ]
                                                      HostConnection::get() New Host Connection established 0xee952bc0, tid 1353
06-09 07:21:38.388 1334-1353/Abc.com I/OpenGLRenderer: Initialized EGL, version 1.4
06-09 07:21:38.464 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:21:38.464 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xee9565a0, error=EGL_SUCCESS
06-09 07:21:42.143 1334-1334/Abc.com I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-09 07:21:42.149 1334-1334/Abc.com I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-09 07:21:42.225 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:21:42.225 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xee956640, error=EGL_SUCCESS
06-09 07:21:42.633 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2150
06-09 07:21:52.584 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:21:52.584 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe9b3d840, error=EGL_SUCCESS
06-09 07:21:53.100 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:21:53.107 1334-1353/Abc.com D/OpenGLRenderer: endAllStagingAnimators on 0xde792000 (RippleDrawable) with handle 0xee952da0
06-09 07:21:54.178 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:21:54.178 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xee9335c0, error=EGL_SUCCESS
06-09 07:21:54.885 1334-1334/Abc.com I/Choreographer: Skipped 39 frames!  The application may be doing too much work on its main thread.
06-09 07:21:55.172 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe9ba5920
06-09 07:21:56.128 1334-1334/Abc.com E/shape: HEART,
06-09 07:21:57.511 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:21:57.511 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xde4491a0, error=EGL_SUCCESS
06-09 07:21:58.203 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:22:02.868 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:22:02.868 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe8d61480, error=EGL_SUCCESS
06-09 07:22:02.927 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:22:02.927 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe8d613a0, error=EGL_SUCCESS
06-09 07:22:02.946 1334-1334/Abc.com E/RecyclerView: No adapter attached; skipping layout
06-09 07:22:03.136 1334-1334/Abc.com E/RecyclerView: No adapter attached; skipping layout
06-09 07:22:03.193 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe9ba5920
06-09 07:22:03.371 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2770
06-09 07:22:03.602 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:22:03.602 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xde257ec0, error=EGL_SUCCESS
06-09 07:22:04.094 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.010 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.010 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>
06-09 07:22:06.010 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.010 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.011 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.011 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.012 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.013 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.017 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfPrinterGraphics2D>
06-09 07:22:06.242 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:22:06.242 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xde0bd5c0, error=EGL_SUCCESS
06-09 07:22:06.401 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2380
06-09 07:22:06.409 1334-1353/Abc.com D/OpenGLRenderer: endAllStagingAnimators on 0xde146600 (RippleDrawable) with handle 0xe8d3b660
06-09 07:22:09.669 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2150
06-09 07:22:16.830 1334-1353/Abc.com W/EGL_emulation: eglSurfaceAttrib not implemented
06-09 07:22:16.830 1334-1353/Abc.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe8d614a0, error=EGL_SUCCESS

here is error Logcat: 这是错误Logcat:

06-09 07:21:37.993 1334-1334/? E/libprocessgroup: failed to make and chown /acct/uid_10011: Read-only file system
06-09 07:21:42.633 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2150
06-09 07:21:53.100 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:21:55.172 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe9ba5920
06-09 07:21:56.128 1334-1334/Abc.com E/shape: HEART,
06-09 07:21:58.203 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:22:02.946 1334-1334/Abc.com E/RecyclerView: No adapter attached; skipping layout
06-09 07:22:03.136 1334-1334/Abc.com E/RecyclerView: No adapter attached; skipping layout
06-09 07:22:03.193 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe9ba5920
06-09 07:22:03.371 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2770
06-09 07:22:04.094 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2620
06-09 07:22:06.401 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2380
06-09 07:22:09.669 1334-1353/Abc.com E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe8ec2150

In your log I see this line: 在您的日志中,我看到以下行:

06-09 07:22:06.009 1334-1334/Abc.com I/art: Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>

Notice awt . 注意awt AWT is a part of Java that is not available on Android. AWT是Java的一部分,在Android上不可用。 You are using iText for Java (which version???) but you should use the Android port instead, iTextG. 您正在使用Java的iText(哪个版本???),但应改用Android端口iTextG。 It is functionally identical to iText, but with all the things stripped out that aren't available on Android. 它在功能上与iText相同,但是剔除了所有在Android上不可用的功能。

iTextG is available on Maven Central: https://mvnrepository.com/artifact/com.itextpdf/itextg so you can use it in Gradle, or whatever your favorite build system is. iTextG在Maven Central上可用: https ://mvnrepository.com/artifact/com.itextpdf/itextg,因此您可以在Gradle或任何您喜欢的构建系统中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM