PAR
PAR

Reputation: 31

Android: iText PdfReader throws IOException not found as file or resource

I have a problem using iText. when I create a PdfReader always the same problem: IOException, not fount as file or resource.

  String ficherosAuxiliaresDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/FG/FicherosInternos/Auxiliares/";

            String src = ficherosAuxiliaresDir + "descuentosform.pdf";
            String dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/FG/FicherosInternos/Descuentos/" + clienteSeleccionado.getIdCliente() + "-Hoja de descuentos-ForestalGarden.pdf";

            //Fuente
            String fuente = ficherosAuxiliaresDir + "robotomonoregular.ttf";

            myFilePdf = new File(dest);

            PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));

however if I try:

File f = new File(src);
boolean b = f.exists();

Here b = TRUE, so file is there, app can access to it....... where is the problem? In fact on my previous code PdfWriter(dest) works fine.

Here the stack trace:

java.io.IOException: /storage/emulated/0/Download/FG/FicherosInternos/Auxiliares/descuentosform.pdf not found as file or resource.
    at com.itextpdf.io.source.RandomAccessSourceFactory.createByReadingToMemory(RandomAccessSourceFactory.java:245)
    at com.itextpdf.io.source.RandomAccessSourceFactory.createBestSource(RandomAccessSourceFactory.java:175)
    at com.itextpdf.kernel.pdf.PdfReader.<init>(PdfReader.java:157)
    at com.itextpdf.kernel.pdf.PdfReader.<init>(PdfReader.java:170)
    at par.fg.forestalgarden2022.Activities.HojaDescuentos.crearPdfPlano(HojaDescuentos.java:1139)
    at par.fg.forestalgarden2022.Activities.HojaDescuentos.lambda$onCreate$42$HojaDescuentos(HojaDescuentos.java:838)
    at par.fg.forestalgarden2022.Activities.-$$Lambda$HojaDescuentos$Fz5cuETvWiFhwCLDUn0PE0ibFVA.onClick(Unknown Source:4)
    at android.view.View.performClick(View.java:7792)
    at android.widget.TextView.performClick(TextView.java:16112)
    at android.view.View.performClickInternal(View.java:7769)
    at android.view.View.access$3800(View.java:910)
    at android.view.View$PerformClick.run(View.java:30218)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8663)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

Anyone can help me? Thank you!!

Upvotes: 2

Views: 998

Answers (1)

PAR
PAR

Reputation: 31

SOLVED

It was a problem of permission. I modified to SDK 31 so I had to ask for a full access permission, this is the reason it works before (SDK26) and fail now. So just added:

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                if (Environment.isExternalStorageManager()) {
                    //todo when permission is granted
                } else {
                    //request for the permission
                    Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                }
            }

And it works great. I just post the solution just in case someone have the same problem.

Upvotes: 2

Related Questions