Home > Software engineering >  The PDF file is not saved in the Android 11 Download folder
The PDF file is not saved in the Android 11 Download folder

Time:03-10

Inside my software, a list of user information is generated offline as a PDF file. I tested this operation on Android 7 and 8 and everything was fine. But when I test on Android 11, the file is not generated. I was looking for a solution but I did not really find the complete source and training in this field.

I was able to create a PDF file via Intent, but inside another software, I saw that as soon as I clicked the save button, a folder with the program name was created in the Documents folder and the file was created inside.

This is the code I use to save the PDF file in the Download folder and it works for Android 7 and 8.

 public void savePdfFileToStorage(String pdfTitleHeader, String currentTime, PdfDocument pdfDocument, Context context) {
        String PdfDir=Environment.getExternalStorageDirectory()   "/Download/Apple";
        File dir=new File(PdfDir);
        if (!dir.exists())
            dir.mkdir();

        String fileName = pdfTitleHeader   "_"   todayDate()   "_"   convertToEnglishDigits(currentTime)   ".pdf";

        File file = new File(PdfDir,fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
                Log.e(TAG, "savePdfFileToStorage: "   "file created"   file.getName()   "path: "   file.getPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            pdfDocument.writeTo(new FileOutputStream(file));
            Log.e(TAG, "savePdfFileToStorage: pdf Wrote in file");
            Toast.makeText(context, "فایل PDF در پوشه Download/Apple حافظه داخلی ذخیره شد.", Toast.LENGTH_LONG).show();

        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(context, "فایل PDF ساخته نشد.", Toast.LENGTH_LONG).show();
        }
        pdfDocument.close();
    }

And I wrote these codes in the Manifest file.

      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      <application>
                    ...
                    android:requestLegacyExternalStorage="true"
                    ...
      </application>

Please advise where I should add the code to solve the storage problem in Android 11.

CodePudding user response:

This code generates a similar name: allTransaction 20220202 10:15:23 .pdf

The : is a forbidden character in file names and paths.

CodePudding user response:

I used the following code and was finally able to save the file in the Documents folder.

 public void savePdfFileToStorage(String pdfTitleHeader, String currentTime, PdfDocument pdfDocument, Context context) {
        File dir;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
            dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)   "/Apple");
        else dir = new File(Environment.getExternalStorageDirectory()   "/Apple");
        if (!dir.exists()) {
            boolean success = dir.mkdirs();
            if (!success)
                dir = null;
        }

        String fileName = pdfTitleHeader   "_"   todayDate()   "_"   convertToEnglishDigits(currentTime)   ".pdf";

        File file = new File(dir, fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
                Log.e(TAG, "savePdfFileToStorage: "   "file created"   file.getName()   "path: "   file.getPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            pdfDocument.writeTo(new FileOutputStream(file));
            Log.e(TAG, "savePdfFileToStorage: pdf Wrote in file");
            Toast.makeText(context, "فایل PDF در پوشه Download/Appleحافظه داخلی ذخیره شد.", Toast.LENGTH_LONG).show();

        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(context, "فایل PDF ساخته نشد.", Toast.LENGTH_LONG).show();
        }
        pdfDocument.close();
    }
  • Related