-1

I have been trying to write PDF files on android ,when i am using internal storage it shows no error but i am unable to see the file created and if i change path to external storage i get permission denied even though i have given permission in my manifest file. I have creating a class which i am calling from activity to write files it fetches data from database. Here is the code.

      public boolean createPdfFile(String path, Employee employee,String month,Context context){
    boolean status=false;
    if(path!=null && employee!=null){
        Document document=new Document();
        try{
            if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
                Toast.makeText(this, "External SD card not mounted", Toast.LENGTH_LONG).show();
            }
            File sdCard=Environment.getExternalStorageDirectory();
            File file=new File(sdCard,month+"_"+employee.getEmployeeName()+".pdf");
            PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream(file));
            writer.setEncryption(employee.getDateOfBirth().getBytes(),employee.getEmployeeName().getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);
            document.open();
            Paragraph p=new Paragraph();
            p.add("Payslip for the month of January");
            p.setFont(new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(1, 1, 1)));
            p.setAlignment(Element.ALIGN_CENTER);
            document.add(p);
            PdfPTable table=new PdfPTable(2);
            table.setWidthPercentage(100); //Width 100%
            table.setSpacingBefore(10f); //Space before table
            table.setSpacingAfter(10f); //Space after table
            float[] columnWidths = {1.5f, 1.5f};
            table.setWidths(columnWidths);
            table.addCell("Employee Code");
            table.addCell(employee.getEmployeeID());
            table.addCell("Department");
            table.addCell(employee.getDepartment().getDepartmentName());
            table.addCell("Date of Birth");
            table.addCell(employee.getDateOfBirth());
            table.addCell("Date of Joining");
            table.addCell(employee.getDateOfJoining());
            table.addCell("Employee Name");
            table.addCell(employee.getEmployeeName());
            document.add(table);
            document.close();
            writer.close();
            status=true;
        }catch (FileNotFoundException e) {
            status=false;
            Log.e("PDF exception aaa",e.getMessage());
        }catch (DocumentException e) {
            status=false;
            Log.e("PDF exception abc",e.getMessage());
        } catch (IOException e) {
            status=false;
            Log.e("PDF exception",e.getMessage());
        }
    }
    return status;
}
user3026516
  • 3
  • 1
  • 3
  • http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – Mike M. Apr 04 '17 at 03:10
  • Possible duplicate of [Android permission doesn't work even if I have declared it](http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it) – Emad Apr 04 '17 at 06:02

1 Answers1

0

If you are developing for API level 23 or newer you need to explicitly ask the user for permission.

See: https://developer.android.com/training/permissions/requesting.html#perm-check

parkgrrr
  • 738
  • 7
  • 12