skip to Main Content

I am developing a multifile converter app , in which user can select a excel file which can be converted into pdf , below method is used to create pdf file that is working fine in my phone which is android pie (android 9 ) , but when I run the app in android 10 real device also in emulator no directory is being created . please help me with the code
and requesting runtime permission with dexter library

I added the line in manifest :-

  android:requestLegacyExternalStorage="true"

Permissions :–

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

My CreatePDF method:-

 void Convert_to_PDF(Uri path) throws IOException, DocumentException, URISyntaxException {

   File f = new File(Environment.getExternalStorageDirectory() + "/MultiFileCon");



    if (!f.exists()) if ( !f.mkdir()) return;




    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH.mm.ss");
    Date date = new Date();
    // System.out.println(formatter.format(date));
    Log.d("DATE", formatter.format(date) + ".pdf");

    File pdffilex = new File(f.getAbsolutePath(), formatter.format(date) + ".pdf");


    Log.d("ABC", "Convert_to_PDF: "+uriforExcel.getPath());


    if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
        Toast.makeText(this, "android version is "+android.os.Build.VERSION.SDK_INT, Toast.LENGTH_LONG).show();
        Log.d("VERSION NAME", "Convert_to_PDF: "+android.os.Build.VERSION.SDK_INT);
        File file = new File(path.getPath());//create path from uri
        final String[] split = file.getPath().split(":");//split the path.
       filePath = split[1];
        input_document = new FileInputStream(filePath);
    }
    else {
        input_document = new FileInputStream(new File(PathUtil.getPath(this,path)));
    }




    // Read workbook into HSSFWorkbook
    HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document);




    // Read worksheet into HSSFSheet
    HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0);
    // To iterate over the rows
    Iterator<Row> rowIterator = my_worksheet.iterator();
    //We will create output PDF document objects at this point
    Document iText_xls_2_pdf = new Document();
    PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream(pdffilex));
    iText_xls_2_pdf.open();
    //we have two columns in the Excel sheet, so we create a PDF table with two columns
    //Note: There are ways to make this dynamic in nature, if you want to.
    PdfPTable my_table = new PdfPTable(7);
    //We will use the object below to dynamically add new data to the table
    PdfPCell table_cell;
    //Loop through rows.
    while(rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        while(cellIterator.hasNext()) {
            Cell cell = cellIterator.next(); //Fetch CELL
            switch(cell.getCellType()) { //Identify CELL type
                //you need to add more code here based on
                //your requirement / transformations
                case Cell.CELL_TYPE_STRING:
                    //Push the data from Excel to PDF Cell
                    table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));
                    Log.d("DOL", cell.getStringCellValue());






                    //feel free to move the code below to suit to your needs
                    my_table.addCell(table_cell);
                    break;
            }
            //next line
        }

        // Log.d("DOL", "Convert_to_PDF: "+datatobe);

    }
    //Finally add the table to PDF document
    iText_xls_2_pdf.add(my_table);
    iText_xls_2_pdf.close();
    //we created our pdf file..
    input_document.close(); //close xls
}

2

Answers


  1. SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

    ':'
    

    is a forbidden character in a file system path.

    Try:.

     "dd-MM-yyyy HH.mm.ss"
    
    Login or Signup to reply.
  2. The Environment.getExternalStorageDirectory() method does not work on Android 11 or above version devices for security reasons.

    You can easily store your file in a public directory. Here is an example of how you access a public directory in Android Studio.

    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

    so you can try this code in your project.

    File baseFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "MultiFileCon");
    
    if (!baseFile.exists()) {
        baseFile.mkdirs();
    }
    
    File pdfFile = new File(baseFile, "PDF_" + Calendar.getInstance().getTimeInMillis() + ".pdf");
    
    Log.e(TAG, "onCreate: pdfFile path is:  " + pdfFile.getAbsolutePath());
    
    • here you got path be like: /storage/emulated/0/Download/MultiFileCon/PDF_1665148985587.pdf
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search