Problem Open PDF generated permission denied

1

I have an application that creates a pdf to then open it and see it on the screen ... It works well on some devices, but on some when it is debugged, the following message

05-22 11:38:43.105 9334-9334/desarrollos.lfpu.com.pos_connect W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Reporte.pdf/MisGenerados/Factura_de_venta_sistevar.pdf: open failed: EACCES (Permission denied)

The program works on my cell phone but when I try the previous message on a Chinese tablet

Code:

 private void generarPDF1(String htmlPDF){
    String tituloPdf="Factura_de_venta_sistevar.pdf";
    Document document = new Document(PageSize.LETTER);
    String NOMBRE_ARCHIVO = tituloPdf.toString();
    String tarjetaSD = Environment.getExternalStorageDirectory().toString();
    File pdfDir = new File(tarjetaSD + File.separator + NOMBRE_CARPETA_APP);
    if(!pdfDir.exists()){
        pdfDir.mkdir();
    }
    File pdfSubDir = new File(pdfDir.getPath() + File.separator + GENERADOS);
    if(!pdfSubDir.exists()){
        pdfSubDir.mkdir();
    }
    nombre_completo = Environment.getExternalStorageDirectory() + File.separator + NOMBRE_CARPETA_APP
            + File.separator + GENERADOS + File.separator +NOMBRE_ARCHIVO;
    File outputfile = new File(nombre_completo);
    if(outputfile.exists()){
        outputfile.delete();
    }
    try {
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(nombre_completo));
        document.open();
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.iconopdf);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        Image imagen = Image.getInstance(stream.toByteArray());
        imagen.getAlignment();
        document.add(imagen);
        document.addAuthor("Sitevar_S.A");
        document.addCreator("Vendedor");
        document.addSubject("");
        document.addCreationDate();
        document.addTitle("Factura_de_ventas");
        String factura = htmlPDF;

        XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
        try{
            worker.parseXHtml(pdfWriter,document, new StringReader(factura));
            document.close();
            Toast.makeText(getContext(), "PDF Generado", Toast.LENGTH_SHORT).show();
             mostrarPdf(nombre_completo,this);
            lanzarEmail();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Method to display the PDF

    private void mostrarPdf(String archivo, Context contex){
    Toast.makeText(getContext(), "Leyendo el archivo", Toast.LENGTH_SHORT).show();
    File file = new File(archivo);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file),"application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        contex.startActivity(intent);

    }catch (ActivityNotFoundException e){
        Toast.makeText(contex, "No tiene una app para abrir este archivo", Toast.LENGTH_SHORT).show();
    }
}

permissions on the manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
asked by Fernando Urrego 22.05.2018 в 18:51
source

1 answer

0

The LogCat indicates denied perms in the message:

  

java.io.FileNotFoundException:   /storage/emulated/0/Reporte.pdf/MisGenerados/Factura_de_venta_sistevar.pdf:   open failed: EACCES (Permission denied)

this is related to the permission WRITE_EXTERNAL_STORAGE that although you comment you have it specified in your file AndroidManifest.xml , the problem you have is that you surely use a device with android 6.0 or higher, for this reason you must require the permission manually.

            //Check permissions Android 6.0+
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                checkExternalStoragePermission();
            }

You can use this method, by accepting the permission your application will work without problems.

private void checkExternalStoragePermission() {
    int permissionCheck = ContextCompat.checkSelfPermission(
            getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {          
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
    } 
}

It is not necessary to declare the permissions READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE , you only need:

    
answered by 22.05.2018 в 20:45