Использую такую конструкцию для преобразования ArrayList list в *.csw файл, для дальнейшей отправки его через Gmail, etc.
// Готовим файл *.csv к отправке
File csvFile = createCSVFIle(filterEventUserList);
String authoritets = fContext.getApplicationContext().getPackageName() + ".fileprovider";
Uri fileUri = FileProvider.getUriForFile(fContext,authoritets,csvFile);
// Создаем интент с экшеном на отправку
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Заполняем данными: тип текста, адрес, сабж и собственно текст письма
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, latestEmail);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, latestSubject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, latestMessage);
emailIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
/* Отправляем на выбор!*/
fContext.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
public File createCSVFIle(ArrayList<String[]> listToSend){
String username = "default";
String fileName = username + "-timeSheetApp.csv";
File exportDir = new File(Environment.getExternalStorageDirectory(),"csv");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, fileName);
try {
file.createNewFile();
CSVWriter writer = new CSVWriter(new FileWriter(file));
for(String[] temp : listToSend){
writer.writeNext(temp);
}
writer.close();
}catch (IOException e){
e.printStackTrace();
}
return file;
}
в манифест добавил
<provider
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false"
android:authorities="${applicationId}.fileprovider">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
так же создал file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="timesheetapp" path="app" />
</paths>
в строке
Uri fileUri = FileProvider.getUriForFile(fContext,authoritets,csvFile);
Получаю
Process: kodman.timesheetapp, PID: 13869 java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/csv/default-timeSheetApp.csv
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости