这是我的从回收视图列表中删除项目的代码。前三行代码可从回收视图中删除该项目。但是,当我再次开始活动时,又出现了已删除的文件,因为它们没有从存储中删除。有人可以帮我吗? 如何从存储中删除所选文件?
private void deleteItem(int index) {
recordingArrayList.remove(index);
notifyItemRemoved(index);
notifyItemRangeChanged(index,recordingArrayList.size());
//remove from storage
File root = android.os.Environment.getExternalStorageDirectory();
String path = root.getAbsolutePath() + "/IRadio/Audios";
File file = new File(path);
file.delete();
}
答案 0 :(得分:0)
尝试下面的代码
File root = android.os.Environment.getExternalStorageDirectory();
String path = root.getAbsolutePath() + "/IRadio/Audios";
File file = new File(path);
file.delete();
if(file.exists()){
file.getCanonicalFile().delete();
if(file.exists()){
getApplicationContext().deleteFile(file.getName());
}
}
确保您的path
确实存在于路径上。
答案 1 :(得分:0)
我是这样做的。现在它可以正常工作,并且代码中的所有内部存储文件也已删除。
private void deleteItem(int index) {
recordingArrayList.remove(index);
notifyItemRemoved(index);
notifyItemRangeChanged(index, recordingArrayList.size());
File root = android.os.Environment.getExternalStorageDirectory();
String path = root.getAbsolutePath() + "/IRadio/Audios/";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
String fileName = files[index].getName();
String recordingUri = root.getAbsolutePath() + "/IRadio/Audios/" + fileName;
File myfile = new File(recordingUri);
myfile.delete();
}