无法使用警报对话框覆盖外部存储中的PDF文件

时间:2019-06-16 15:53:37

标签: android

导出新的PDF文件时没有错误,但是当我尝试使用警报对话框覆盖PDF文件时,它使活动崩溃。

我尝试过将文件写入部分作为函数移出,也将其保留在代码中,但是两者都不起作用。

if(imageIV.getDrawable() != null && nameOfPDF != null){
            String PDFname = nameOfPDF.getText().toString();
            Bitmap bitmap = ((BitmapDrawable) imageIV.getDrawable()).getBitmap();

            final PdfDocument pdfDocument = new PdfDocument();
            PdfDocument.PageInfo pi = new PdfDocument.PageInfo.Builder(bitmap.getWidth(),bitmap.getHeight(),1).create();

            PdfDocument.Page page = pdfDocument.startPage(pi);
            Canvas canvas = page.getCanvas();
            Paint paint = new Paint();
            paint.setColor(Color.parseColor("#FFFFFF"));
            canvas.drawPaint(paint);

            bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(),true);
            paint.setColor(Color.BLUE);
            canvas.drawBitmap(bitmap, 0, 0, null);

            pdfDocument.finishPage(page);

            if(!nameOfPDF.getText().toString().contains(".pdf")){
                PDFname += ".pdf";
            }

            final File file = new File(root, PDFname);
            if(file.exists()){
                new AlertDialog.Builder(this)
                        .setTitle("File Already Exist")
                        .setMessage("Do you want to overwrite the existing file?")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                try{
                                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                                    pdfDocument.writeTo(fileOutputStream);
                                    Toast.makeText(TextRecognition.this,"Successfully overwritten", Toast.LENGTH_SHORT).show();
                                }catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .show();

            }else{
                try{
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    pdfDocument.writeTo(fileOutputStream);
                    Toast.makeText(this,"File does not exist, successfully saved", Toast.LENGTH_SHORT).show();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }

            pdfDocument.close();
        }

这就是我得到的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.textscannerandconverter, PID: 3777
    java.lang.IllegalStateException: document is closed!
        at android.graphics.pdf.PdfDocument.throwIfClosed(PdfDocument.java:225)
        at android.graphics.pdf.PdfDocument.writeTo(PdfDocument.java:170)
        at com.example.textscannerandconverter.TextRecognition$7.onClick(TextRecognition.java:290)
        at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:6523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

0 个答案:

没有答案