如何解决发送后删除文件android studio
文件发送正常,但如何在发送后删除它,deleteOnExit() 不t work. Maybe I should save it in cache but when I
我试图这样做(使用 getCacheDir())文件不发送
任何帮助将不胜感激
String fileName_Kon;
fileName_Kon = ((EditText) popupWindow.getContentView().findViewById(R.id.userInputNameForFile)).getText().toString() + ((TextView) popupWindow.getContentView().findViewById(R.id.dateNameFile)).getText().toString();
File sharedir = new File(getFilesDir(),"files");
if (!sharedir.exists()) sharedir.mkdir();
String shareName = new String(fileName_Kon);
try {
File shareFile = File.createTempFile(shareName,".txt",sharedir);
} catch (IOException e) {
e.printstacktrace();
}
//shareFile.deleteOnExit();
try {
FileOutputStream flogs = new FileOutputStream(shareFile);
flogs.write("text").getBytes());
flogs.flush();
flogs.close();
} catch (IOException e) {
e.printstacktrace();
}
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("message/rfc822");
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(MainActivity.this,AUTHORITY,shareFile);
ArrayList uris = new ArrayList<Uri>();
uris.add(contentUri);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
MainActivity.this.startActivity(Intent.createChooser(emailIntent,"Send..."));
解决方法
等待您发送文件的应用程序活动完成,然后删除文件。
- 注册请求代码(作为类的字段):
private static final int REQUEST_CODE = 10;
- 将文件保存在应用私有文件夹中:
try {
File shareFile = new File(sharedir,shareName + ".txt");
shareFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
// write to file,as you do in the snippet
- 启动 Activity,并在用户从所选应用返回时通知结果:
MainActivity.this.startActivityForResult(
Intent.createChooser(emailIntent,"Send..."),REQUEST_CODE
);
- 对结果执行操作:
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
if (requestCode == REQUEST_CODE) {
// delete file here
File shareFile = new File(sharedir,shareName + ".txt");
if (shareFile.exists()) {
shareFile.delete();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。