Ogni tanto (raramente) puo' essere necessario creare un report in una applicazione Android (anche se questa operazione e' piu' tipicamente indirizzata ad applicazione Desktop)
La soluzione piu' semplice e' appoggiarsi ad una libreria per la generazione di PDF come iTextPdf
per configurare le dipendenze si deve includere in build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations' })
compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:design:26.+'
//dipendenza PDF compile 'com.itextpdf:itextg:5.5.9'
testCompile 'junit:junit:4.12'}
Fatto cio' si puo' procedere come in seguito. L'unico aspetto di interesse e' che quando si lancia l'intent per la visualizzazione del Pdf, a causa della gestione dei permessi, si deve gestire l'errore
android.os.FileUriExposedException
---------------------------------------------------------------------------------------
package com.example.lucainnocenti.photonotation;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ReportActivity extends AppCompatActivity {
private Button pulsante;
private FileOutputStream output;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
pulsante = (Button) findViewById(R.id.buttonpdf);
pulsante.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
try {
try {
crea_pdf();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
});
}
private void crea_pdf() throws IOException, DocumentException {
File pdfFolder = new File(Environment.getExternalStorageDirectory().getPath(), "Photonotation");
if (!pdfFolder.exists()) {
pdfFolder.mkdir();
Log.i("Photonotation", "Pdf Directory created");
}
//Create time stamp Date date = new Date() ;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);
String percorso = pdfFolder.getAbsolutePath() + "/" + timeStamp +".pdf";
Log.i("Photonotation", percorso);
File myFile = new File(percorso);
output = new FileOutputStream(percorso);
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, output);
document.open();
document.add(new Paragraph("Luca"));
String path = "/storage/emulated/0/Photonotation/20171031_140238.jpg";
Image image = Image.getInstance(path);
document.add(image);
document.close();
// visualizza il pdf prodotto
// le prime righe servono ad evitare android.os.FileUriExposedException
if(Build.VERSION.SDK_INT>=24){
try{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e){
e.printStackTrace();
}
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(myFile), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
}