[英]Cannot get file in Download directory to delete using File.delete() on android studio
[英]Delete a file in download directory
几个月前我被帮助创建了一个应用程序,我正试图让它自己进化,但这并不像我想的那么容易。
我想删除已在下载目录中创建的文件。 我使用这个文件夹作为缓存文件夹来分享存储在我的应用程序中的声音。 这是共享选项的示例(效果很好,除了我不明白应该将outputFile.delete();
行放在哪里...)
case R.id.partage:
File outputFile = new File("");
InputStream is = getResources().openRawResource(((Sound) adapter.getItem(index)).getMpsound());
try {
byte[] buffer = new byte[is.available()];
is.read(buffer);
File outputDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
outputFile = new File(outputDir, getResources().getResourceEntryName(((Sound) adapter.getItem(index)).getMpsound()) + ".mp3");
OutputStream os = new FileOutputStream(outputFile);
os.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", outputFile));
share.putExtra(Intent.EXTRA_TEXT, "\"" + ((Sound) adapter.getItem(index)).getTitre_show() + "\" shared by my app");
startActivity(Intent.createChooser(share, "Share Sound File"));
return true;
任何想法?
由于您将文件共享给其他应用程序,因此在其他应用程序读取您的文件之前,您无法删除您的文件。 没有什么可以告诉你其他人已经完成了阅读你的文件。 可以批量删除。 例如,您可以一起删除当前时间前 1 小时或更长时间共享的所有文件。 但为此,您必须创建文件以便能够跟踪它们。 您可以将所有文件保存在下载目录中的一个公用文件夹中,以便于跟踪这些文件。 如果您需要批量删除的逻辑和代码方面的帮助,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.