我需要删除存储在应用程序数据库空间中的所有数据库。我使用以下路径存储它们:context.getDatabasePath(“db1”)。getAbsolutePath()
我有很多数据库,有随机名称,所以我不知道名字,我只是想删除所有这些。
我试着用这个:
String filesDir = ApplicationContextProvider.getContext().getFilesDir().getAbsolutePath();
File cache = new File(filesDir);
if (cache.isDirectory()) {
String[] children = cache.list();
for (int i = 0; i < children.length; i++) {
System.out.println("Deleting: "+children[i]);
new File(cache, children[i]).delete();
}
}
但它不起作用。数据库仍在那里。
答案 0 :(得分:1)
您不需要知道该路径。只需使用您可以删除它们的数据库列表。
for (String databaseName : context.databaseList()) {
context.deleteDatabase(databaseName);
}
如果你真的需要它
File databasesPath = context.getDatabasePath("ignored").getParentFile();
您通过getFilesDir
获得的路径不同。