first post here.
I have read all similar posts without finding any answer, i'm a newbie at dev in general.
Here is what i'm looking for:
On button click, the code generates a PDF with some info, and i just want to send the created pdf by mail. First problem is when i choose an email app to send the mail, i got a message "Impossible to attach file". Second problem is that the created file is not readable on the phone while it's readable on PC.
By searching i found out that it may be in relation with permission. I tried setReadable
method without result. Android studio makes a hint (result of setReadable is ignored).
Code :
public class VerifList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verif_list);
//Variables
//Strings
final String name;
final String myFormat;
//Others
final Calendar calendar;
final SimpleDateFormat sdf;
final String datePdf;
//Buttons
Button endControlButton;
//TextView
TextView title;
TextView date;
//Edit Text
final EditText oil_TV;
final EditText engine_liquid_TV;
EditText wipers_liquid_TV;
EditText breaks_liquid_TV;
EditText windows_TV;
EditText wipers_TV;
EditText mirrors_TV;
EditText numberplate_TV;
EditText frontTires_TV;
EditText rearTires_TV;
EditText tiresState_TV;
EditText light1_TV;
EditText light2_TV;
EditText light3_TV;
EditText light4_TV;
EditText light5_TV;
EditText light6_TV;
EditText light7_TV;
EditText interiorClean_TV;
EditText exteriorClean_TV;
EditText jacket_TV;
EditText triangle_TV;
EditText strap_TV;
EditText extinguisher_TV;
//Set title text
title = findViewById(R.id.title_TV);
Bundle extras = getIntent().getExtras();
assert extras != null;
name = extras.getString("name");
title.setText(name);
//Display date
date = findViewById(R.id.date_TV);
calendar = Calendar.getInstance();
myFormat = "dd/MM/yy";
sdf = new SimpleDateFormat(myFormat, Locale.FRANCE);
date.setText("Date du contrôle : " + sdf.format(calendar.getTime()));
datePdf = calendar.getTime().toString();
//Variables declaration
oil_TV = findViewById(R.id.oil_TV);
engine_liquid_TV = findViewById(R.id.engine_liquid_TV);
wipers_liquid_TV = findViewById(R.id.wipers_liquid_TV);
breaks_liquid_TV = findViewById(R.id.breaks_liquid_TV);
windows_TV = findViewById(R.id.windows_TV);
wipers_TV = findViewById(R.id.wipers_TV);
mirrors_TV = findViewById(R.id.mirrors_TV);
numberplate_TV = findViewById(R.id.numberplate_TV);
frontTires_TV = findViewById(R.id.frontTires_TV);
rearTires_TV = findViewById(R.id.rearTires_TV);
tiresState_TV = findViewById(R.id.tiresState_TV);
light1_TV = findViewById(R.id.light1_TV);
light2_TV = findViewById(R.id.light2_TV);
light3_TV = findViewById(R.id.light3_TV);
light4_TV = findViewById(R.id.light4_TV);
light5_TV = findViewById(R.id.light5_TV);
light6_TV = findViewById(R.id.light6_TV);
light7_TV = findViewById(R.id.light7_TV);
interiorClean_TV = findViewById(R.id.interiorClean_TV);
exteriorClean_TV = findViewById(R.id.exteriorClean_TV);
jacket_TV = findViewById(R.id.jacket_TV);
triangle_TV = findViewById(R.id.triangle_TV);
strap_TV = findViewById(R.id.strap_TV);
extinguisher_TV = findViewById(R.id.extinguisher_TV);
//Création de listes
final ArrayList textViewArray = new ArrayList(24);
//TextViews
textViewArray.add(oil_TV);
textViewArray.add(engine_liquid_TV);
textViewArray.add(wipers_liquid_TV);
textViewArray.add(breaks_liquid_TV);
textViewArray.add(windows_TV);
textViewArray.add(wipers_TV);
textViewArray.add(mirrors_TV);
textViewArray.add(numberplate_TV);
textViewArray.add(frontTires_TV);
textViewArray.add(rearTires_TV);
textViewArray.add(tiresState_TV);
textViewArray.add(light1_TV);
textViewArray.add(light2_TV);
textViewArray.add(light3_TV);
textViewArray.add(light4_TV);
textViewArray.add(light5_TV);
textViewArray.add(light6_TV);
textViewArray.add(light7_TV);
textViewArray.add(interiorClean_TV);
textViewArray.add(exteriorClean_TV);
textViewArray.add(jacket_TV);
textViewArray.add(triangle_TV);
textViewArray.add(strap_TV);
textViewArray.add(extinguisher_TV);
//Déclaration du bouton Fin de controle
endControlButton = findViewById(R.id.end_control_button);
endControlButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetWorldReadable")
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View view) {
//Création du pdf
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300,600,1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setTextSize(30);
canvas.drawText(name,50,30,paint);
for (int i = 0 ; i<textViewArray.size();i++){
EditText editText = (EditText) textViewArray.get(i);
String editTextDesc = (String) editText.getContentDescription();
String content = editText.getText().toString();
Boolean isEmpty = content.isEmpty();
if (!isEmpty) {
paint.setTextSize(10);
paint.setColor(Color.RED);
canvas.drawText(editTextDesc + " : " + content, 50 , 50 + (20 * i),paint);
} else {
paint.setTextSize(10);
paint.setColor(Color.BLACK);
canvas.drawText(editTextDesc + " : OK",50, 50 + (20 * i),paint);
}
}
document.finishPage(page);
String directory_path = Environment.getExternalStorageDirectory().getPath() + "/ATTM
Vehicules/";
File file = new File(directory_path);
if (!file.exists()){
file.mkdirs();
}
String targetPdf = directory_path + name + datePdf +".pdf";
File filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
Toast.makeText(getApplicationContext(), "Envoyé", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("main", "error "+e.toString());
Toast.makeText(getApplicationContext(), "Erreur" + e.toString(), Toast.LENGTH_LONG).show();
}
document.close();
//Mail
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "email@hotmail.fr" });
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "test ");
shareIntent.putExtra(Intent.EXTRA_TEXT, "test");
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(targetPdf));
startActivity(shareIntent);
}
});
}
}
Any help would be appreciated. Thanks for reading!