java删除下载目录中的文件
几个月前我被帮助创建了一个应用程序,我正试图让它由我自己进化,但这并不像我想的那么容易
我想删除在下载目录中创建的文件。
我将此文件夹用作缓存文件夹,以共享储存在我应用程序中的声音。
这是股票期权的示例(这很有效,只是我不明白我应该把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 楼答案
由于您正在将文件共享给其他应用程序,因此在其他应用程序读取您的文件之前,您无法删除您的文件。没有任何东西可以告诉你其他人读过你的文件。你可以批量删除。例如,您可以在当前时间前1个多小时删除共享的所有文件。但为此,您必须创建文件,以便能够跟踪它们。您可以将所有文件保存在下载目录中的公用文件夹中,以便于跟踪这些文件。如果您需要有关批量删除的逻辑和代码的帮助,请告诉我