У меня есть приложение для Android, которое создает CSV-файл. И я хочу этим поделиться: отправить на почту, открыть в Excel и так далее.
Читаю туториалы: настраиваем файлообменник и расшариваем файл, но не могу понять: мой это вариант или нет?
Я объявляю манифест:
Код: Выделить всё
android:name="android.support.v4.content.FileProvider"
android:authorities="com.nam1.name2.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
Можете ли вы рассказать мне какой-нибудь простой пример кода (возможно, git-repo), как поделиться файлом с другими приложениями ?
Спасибо!
РЕДАКТИРОВАНИЕ: я пишу новый код для обмена файлами (см. пример FileProvider)
Код: Выделить всё
public class ExportActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_export);
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setAction(Intent.ACTION_SEND);
File imagePath = new File(getApplicationContext().getFilesDir(), "statistics");
File newFile = new File(imagePath, "stat.csv");
CSVHelper csvHelper = new CSVHelper(SharedData.AllPlayersColl);
try {
String path=imagePath.getAbsolutePath();
csvHelper.SaveToCSV(path);
} catch (IOException e) {
e.printStackTrace();
}
Context context=getApplicationContext();
Uri contentUri = getUriForFile(context, "com.name1.name2.fileprovider", newFile);
List resInfoList = getApplicationContext().getPackageManager()
.queryIntentActivities(intentShareFile, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getApplicationContext()
.grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intentShareFile.setData(contentUri);
intentShareFile.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
setResult(100, intentShareFile);
startActivityForResult(intentShareFile,100);
}
}
Код: Выделить всё
fun SaveToCSV(fileName: String?) {
if (fileName == null)
throw NullPointerException("fileName is null!")
val writer = CSVWriter(FileWriter(fileName), '\t')
// make csv file
writer.writeNext(firstLine);
}
Код: Выделить всё
Код: Выделить всё
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND dat=content://com.name1.name2.fileprovider/stat/stat.csv flg=0x2 }
Подробнее здесь: https://stackoverflow.com/questions/414 ... e-csv-file
Мобильная версия