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" />