Reputation: 1332
I made an App that creates a *.csv file inside a folder (made by the same app) and at the same time is inside the Android's Download Folder.
The file can be shared with a ShareIntent, the problem is when the app runs in Android Oreo and the user tries to create the *.csv file, the apps crashes, the thrown exception is:
android.os.FileUriExposedException:
at android.os.StrictMode.onFileUriExposed (StrictMode.java:1958)
at android.net.Uri.checkFileUriExposed (Uri.java:2348)
at android.content.ClipData.prepareToLeaveProcess (ClipData.java:941)
at android.content.Intent.prepareToLeaveProcess (Intent.java:9735)
at android.content.Intent.prepareToLeaveProcess (Intent.java:9741)
at android.content.Intent.prepareToLeaveProcess (Intent.java:9720)
at android.app.Instrumentation.execStartActivity (Instrumentation.java:1795)
at android.app.Activity.startActivityForResult (Activity.java:4495)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult (BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:79)
at android.app.Activity.startActivityForResult (Activity.java:4453)
at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:859)
at android.app.Activity.startActivity (Activity.java:4814)
at android.app.Activity.startActivity (Activity.java:4782)
at com.bpl.spart.bloodpressurelogbook.MainActivity$createReportTask.onPostExecute (MainActivity.java:517)
at com.bpl.spart.bloodpressurelogbook.MainActivity$createReportTask.onPostExecute (MainActivity.java:420)
I know that I should use FileProvider to create Uri's, in API level > 24,but how Can I use the FileProvider to give access from a file inside the Downloads folder?
This is my current code:
Intent ShareIntent = new Intent(android.content.Intent.ACTION_SEND);
ShareIntent.setType("text/csv");
ShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {getString(R.string.reporteTomasDePresion)});
ShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, nombreReporte);
ShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(Intent.createChooser(ShareIntent, getString(R.string.compartirReporte)));
Where "f" is the reference to the .csv file.
public File getDocumentsStorageDir(String directorio){
File file = new File(getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS),directorio);
if(!file.mkdir()){
Log.e("StatusArchivo","Directorio no creado");
}
return file;
}
File file = getDocumentsStorageDir(REPORTDIR);
Calendar cal = Calendar.getInstance();
//sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm");
nombreReporte = "BPReport"+sdf.format(cal.getTime())+".csv";
f = new File(file.getAbsolutePath(),nombreReporte);
String csvFIle = "";
String sistolicaS;
String diastolicaS;
String pulsoS;
String posicionS;
String extremidadS;
String notaS;
String fechaS;
if(params[0].getCount() > 0 ) {
try {
OutputStreamWriter fout = new OutputStreamWriter(new FileOutputStream(f), StandardCharsets.ISO_8859_1);
String nombre = getString(R.string.Nombre);
String nombreDeUsuario = sharedPref.getString("nombreDelUsuario","");
String edad = getString(R.string.Edad);
String edadUsuario = sharedPref.getString("edadDelUsuario","");
String peso = getString(R.string.Peso);
String pesoUsuario = sharedPref.getString("pesoDelUsuario","");
String UnidadDePeso = sharedPref.getString("unidadPeso",getString(R.string.kilos));
String enfermedades = getString(R.string.Enfermedad);
String enfermedadesUsuario = sharedPref.getString("enfermedadesDelUsuario","");
String linea1 = nombre+": ," + nombreDeUsuario + "\n";
fout.write(linea1);
String linea2 = edad+": ," + edadUsuario + "\n";
fout.write(linea2);
String linea3 = peso+": ," + pesoUsuario + UnidadDePeso + "\n";
fout.write(linea3);
String linea4 = enfermedades+": ," + enfermedadesUsuario + "\n";
fout.write(linea4);
String tituloColumnas = getString(R.string.presionsistolica) + "," + getString(R.string.presiondiastolica) + "," + getString(R.string.pulsotil) + "," + getString(R.string.posiciontil) + "," + getString(R.string.extremidadtil) + "," + getString(R.string.notatil) + "," + getString(R.string.fechatil) + "\n";
fout.write(tituloColumnas);
params[0].moveToFirst();
if (params[0].moveToFirst()) {
for (int j = 0; j < params[0].getCount(); j++) {
sistolicaS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.SISTOLICA));
diastolicaS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.DIASTOLICA));
pulsoS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.PULSO));
posicionS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.POSICION));
extremidadS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.EXTREMIDAD));
notaS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.NOTA));
fechaS = params[0].getString(params[0].getColumnIndex(Contrato.Columnas.FECHA));
Log.v("CSV1", csvFIle);
csvFIle = "\"" + sistolicaS + "\",\"" + diastolicaS + "\",\"" + pulsoS + "\",\"" + posicionS + "\",\"" + extremidadS + "\",\"" + notaS + "\",\"" + fechaS + "\"\n";
Log.v("CSV2", csvFIle);
fout.write(csvFIle);
params[0].moveToNext();
}
}
fout.close();
c.close();
Upvotes: 5
Views: 5831
Reputation: 1332
I found the answer by my self, In my paths.xml file I added:
<?xml version="1.0" encoding="utf-8"?>
<paths
xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="Descarga"
path="Download/"/>
<files-path name="nombre"
path="reportes" />
</paths>
And in the Java:
I changed:
ShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(Intent.createChooser(ShareIntent,getString(R.string.compartirReporte)));
For:
Uri fileUri = FileProvider.getUriForFile(MainActivity.this,"com.bpl.spart.bloodpressurelogbook.fileprovider",f);
ShareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
startActivity(Intent.createChooser(ShareIntent, getString(R.string.compartirReporte)));
Also the FileUriExposedException problem in Android Oreo is gone now.
Upvotes: 4