这是我的代码,用于从回收视图列表中删除项目。前三行代码用于从回收视图中删除项目。但是,当我再次开始我的活动时,删除的文件再次出现,因为它们没有从存储中删除。有人能帮我做到这一点吗?如何从存储中删除选定的文件?
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();
}发布于 2019-01-02 14:52:52
尝试下面的代码
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 确实存在于路径上。
发布于 2019-01-04 17:01:08
我就是这样做的。它现在运行良好,内部存储中的文件也被代码删除。
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();
}https://stackoverflow.com/questions/54002278
复制相似问题