Quiero enviar un pdf creado en la app por whatsapp en un android version 13 y con
android {
compileSdkVersion 33
buildToolsVersion '30.0.3'
}
defaultConfig {
minSdkVersion 26
targetSdkVersion 33
}
Lo que ocurre es que whatsapp se abre e inicia la conversación con le teléfono dado pero no hace nada para mandar el pdf. El pdf lo crear bien y además he intentado con éxito enviarlo directamente por whatsapp desde whatsapp.
Para pedir permiso:
checkExternalStoragePermission();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
checkExternalStoragePermission();
} else {
crearpdf();
}
private void checkExternalStoragePermission() {
int permissionCheck = ContextCompat.checkSelfPermission(
getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
} else {
crearpdf();
}
}
Este es el codigo es el siguiente:
@RequiresApi(api = Build.VERSION_CODES.N)
private void crearpdf() {
try {
Document document = new Document( PageSize.LETTER );
String ARCHIVO = "Recibo_" + gmportadores + ".pdf";
File destinoPath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), CARPETA);
if (!destinoPath.exists()) { destinoPath.mkdir(); }
File pdfSubDir = new File(destinoPath + File.separator + RECIBOS);
if (!pdfSubDir.exists()) { pdfSubDir.mkdir(); }
File nombre_completo = new File( pdfSubDir + File.separator + ARCHIVO);
if (!nombre_completo.exists()) { nombre_completo.delete(); }
PdfWriter pdfwrite = PdfWriter.getInstance( document, new FileOutputStream( nombre_completo.toString() ) );
SimpleDateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy", Locale.getDefault() );
Date date = new Date();
String swfecha = dateFormat.format( date );
// Creamos el documento para escribirlo
document.open();
document.addTitle( "Recibo Portador Cristo de las Injurias" );
XMLWorkerHelper xmlWorkerHelper = XMLWorkerHelper.getInstance();
String htmlToPdf = "<html>\n" +
"<style type=\"text/css\">\n" +
".Verdana {\n" +
"\tfont-family: Tahoma, Geneva, sans-serif;\n" +
"}\n" +
".Arial {\n" +
"\tfont-family: Arial, Helvetica, sans-serif;\n" +
"}\n" +
"</style>\n" +
"<body>\n" +
" <table width=\"545\" height=\"316\" border=\"10\" align=\"center\" cellpadding=\"5\" cellspacing=\"2\">\n" +
" <tr>\n" +
" <td width=\"198\" rowspan=\"2\" align=\"center\"><font color=\"#130f40\"><h1>Recibo</h1></font></td>\n" +
" <td width=\"69\"><font color=\"#990000\"><strong>Fecha</strong></font></td>\n" +
" <td width=\"211\" align=\"center\"><font color=\"#00a8ff\"><strong>" + swfecha + "</strong></font></td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td width=\"69\"><strong><font color=\"#990000\">Importe</font></strong></td>\n" +
" <td width=\"211\" align=\"center\"><font color=\"#00a8ff\"><strong>" + cuotatext.getText().toString() + "</strong></font></td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td colspan=\"3\" align=\"center\"><h1><font color=\"#ff5252\" class=\"Verdana\">Stmo. Cristo de las Injurias</font></h1></td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td colspan=\"3\">\n" +
" <div align=\"justify\"><h3><font color=\"#00a8ff\" class=\"Arial\"><strong><em>" + gmportadores + "</em></strong></font></h3></div>\n" +
" <div align=\"justify\"><h3> Como portador del Stmo. Cristo de las Injurias ha abonado la cantidad arriba indicada en concepto de couta voluntaria del año: " + gmanno + "</h3></div>\n" +
" </td>\n" +
" </tr>\n" +
" </table>\n" +
" </body>\n" +
"</html>\n";
try {
xmlWorkerHelper.parseXHtml( pdfwrite, document, new StringReader(htmlToPdf) );
document.close();
try {
document.close();
Intent intent = new Intent("android.intent.action.MAIN");
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/pdf");
intent.setPackage("com.whatsapp");
String numTelefono = "34" + gmtelefonos;
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(nombre_completo.toString()));
intent.putExtra("jid", numTelefono + "@s.whatsapp.net"); //numero telefonico sin prefijo "+"!
startActivity(intent);
} catch (ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "Whatsapp no esta instalado.", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
AlertDialog.Builder alertDialogCopiaError = new AlertDialog.Builder(getActivity());
alertDialogCopiaError.setMessage("No se pudo realizar el PDF. Intentelo más tarde.")
.setTitle("ERROR Creando PDF")
.setIcon(R.drawable.pdf48)
.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
});
alertDialogCopiaError.show();
}
} catch (DocumentException e) {
AlertDialog.Builder alertDialogCopiaError = new AlertDialog.Builder(getActivity());
alertDialogCopiaError.setMessage("No se pudo realizar el PDF. Intentelo más tarde.")
.setTitle("ERROR Creando PDF")
.setIcon(R.drawable.pdf48)
.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
});
alertDialogCopiaError.show();
} catch (FileNotFoundException e) {
AlertDialog.Builder alertDialogCopiaError = new AlertDialog.Builder(getActivity());
alertDialogCopiaError.setMessage("No se pudo realizar el PDF. Intentelo más tarde.")
.setTitle("ERROR Creando PDF")
.setIcon(R.drawable.pdf48)
.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
});
alertDialogCopiaError.show();
}
}
¿Cómo lo puedo solucionar? Gracias
Esto es lo que me sale en el LogCat:
2023-05-04 08:36:24.286 1405-8545/? E/BinderTransaction: Binder proc file not exist.
Asi lo he preparado y sigue igual:
File dir = new File((Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/PDF"));
File file = new File(dir, ARCHIVO);
Uri uri = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".provider", file, ARCHIVO);
String numTelefono = "34" + gmtelefonos;
final Intent compartir = new Intent(android.content.Intent.ACTION_SEND)
//.putExtra(Intent.EXTRA_EMAIL, destinatarios)
.putExtra(Intent.EXTRA_SUBJECT, "Envio de archivos PDF")
.putExtra(Intent.EXTRA_TEXT, "Se adjunta archivo")
.putExtra(Intent.EXTRA_STREAM, uri)
.putExtra("jid", numTelefono + "@s.whatsapp.net")
.setPackage("com.whatsapp")
.setType("application/pdf");
startActivity(Intent.createChooser(compartir, "Compartir PDF"));