In my Android Java application I created a txt file and I can share it. But I want to do that when user click the button, start download that txt file which inside of my application. How can I do this.
Thank you
public void backupPhone(){
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int year = calendar.get(Calendar.YEAR);
String fileName = "StockProgramDatas-"+day+"-"+month+"-"+year+".txt";
//String fileName = "StockProgramDatas.txt";
String filePath = "StockDatas";
if (!isExternalStorageAvaibleForRW()){
Toast.makeText(MainActivity.this,getResources().getString(R.string.main_activity_backup_permission),Toast.LENGTH_SHORT).show();
}
try {
File myExternalFile = new File(getExternalFilesDir(filePath),fileName);
FileOutputStream fos = new FileOutputStream(myExternalFile);
OutputStreamWriter writer = new OutputStreamWriter(fos);
for (Products p: productsArrayList) {
writer.write(p.getProduct_name()+"-"+p.getProduct_brand()+"-"+
p.getProduct_color()+"-"+p.getProduct_amount()+"-"+p.getProduct_sold()+"
");
}
writer.close();
Intent intentShare = new Intent(Intent.ACTION_SEND);
intentShare.putExtra(Intent.EXTRA_TEXT,"Database Backup "+day+"/"+
month+"/"+year);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
Uri uri = FileProvider.getUriForFile(MainActivity.this,"com.nczsoftware.simplestockprogram",myExternalFile);
intentShare.putExtra(Intent.EXTRA_STREAM,uri);
}else {
intentShare.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(myExternalFile));
}
intentShare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentShare.setType("txt/*");
startActivity(Intent.createChooser(intentShare,getResources().getString(R.string.main_activity_backup_share)));
}catch (Exception e){
Log.e("hata",String.valueOf(e));
Toast.makeText(MainActivity.this,getResources().getString(R.string.main_activity_backup_permission_file)+e,Toast.LENGTH_LONG).show();
}
}
question from:
https://stackoverflow.com/questions/65918708/android-java-txt-file-download-from-inside-of-my-application 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…