Android 删除数据库文件夹内部存储
我正在尝试为我的应用程序创建重置应用程序功能,并尝试从内部存储中删除所有文件,包括Android 删除数据库文件夹内部存储,android,file,delete-file,Android,File,Delete File,我正在尝试为我的应用程序创建重置应用程序功能,并尝试从内部存储中删除所有文件,包括文件文件夹、数据库文件夹和共享首选项。问题是,并非每次我试图删除这些文件夹时,它们都会被删除。有时,我用来删除文件的函数返回false。在这种情况下,我的应用程序无法正常工作。以下是我正在使用的: @SuppressWarnings("static-access") public void deleteAllData(){ String cache = this.getCacheDir().toStri
文件文件夹
、数据库文件夹
和共享首选项
。问题是,并非每次我试图删除这些文件夹时,它们都会被删除。有时,我用来删除文件的函数返回false。在这种情况下,我的应用程序无法正常工作。以下是我正在使用的:
@SuppressWarnings("static-access")
public void deleteAllData(){
String cache = this.getCacheDir().toString();
Log.e("","dbPath : "+cache);
File ChFolder = new File(cache);
boolean cachee = deleteDirectory(ChFolder);
Log.e("","Database Folder Delete Success : "+cachee);
String server = rpc.getCurrentServerName(this);
int userId = rpc.getUserId(this);
String userDbPath = "/data/data/"+this.getPackageName()+"/databases/"+Integer.toString(userId)+"_"+server;
File userDbFile = new File(userDbPath);
boolean userDbFileTF = userDbFile.delete();
Log.e("","user Database Folder : "+userDbFileTF);
String sysDbPath = "/data/data/"+this.getPackageName()+"/databases/stampii_sys_tpl.sqlite";
File sysDbFile = new File(sysDbPath);
boolean sysDbFileTF = sysDbFile.delete();
Log.e("","user Database Folder : "+sysDbFileTF);
// Delete Databases Folder :
String dbPath = "/data/data/"+this.getPackageName()+"/databases/";
Log.e("","dbPath : "+dbPath);
File dbFolder = new File(dbPath);
boolean dbFold = deleteDirectory(dbFolder);
Log.e("","Database Folder Delete Success : "+dbFold);
// Delete Files Folder :
String name = this.getFilesDir().toString()+"/users/";
Log.e("","path : "+name);
File files = new File(name);
boolean filesFol = deleteDirectory(files);
Log.e("","filesFol : "+filesFol);
}
static public boolean deleteDirectory(File path) {
if( path.exists() ) {
File[] files = path.listFiles();
if (files == null) {
return true;
}
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
return( path.delete() );
}
@SuppressWarnings(“静态访问”)
public void deleteAllData(){
字符串缓存=this.getCacheDir().toString();
Log.e(“,”dbPath:“+cache”);
文件ChFolder=新文件(缓存);
布尔缓存=删除目录(ChFolder);
Log.e(“,”数据库文件夹删除成功:“+cache”);
String server=rpc.getCurrentServerName(此);
int userId=rpc.getUserId(this);
字符串userDbPath=“/data/data/”+this.getPackageName()+“/databases/”+Integer.toString(userId)+“”+服务器;
File userDbFile=新文件(userDbPath);
布尔值userDbFileTF=userDbFile.delete();
Log.e(“,”用户数据库文件夹:“+userDbFileTF”);
字符串sysDbPath=“/data/data/”+this.getPackageName()+”/databases/stampii_sys_tpl.sqlite”;
文件sysDbFile=新文件(sysDbPath);
布尔值sysDbFileTF=sysDbFile.delete();
Log.e(“,”用户数据库文件夹:“+sysDbFileTF”);
//删除数据库文件夹:
字符串dbPath=“/data/data/”+this.getPackageName()+”/databases/”;
Log.e(“,”dbPath:“+dbPath”);
File dbFolder=新文件(dbPath);
布尔值dbFold=deleteDirectory(dbFolder);
Log.e(“,”数据库文件夹删除成功:“+dbFold”);
//删除文件夹中的文件:
字符串名称=this.getFilesDir().toString()+“/users/”;
Log.e(“,”路径:“+名称);
文件文件=新文件(名称);
布尔filesFol=deleteDirectory(文件);
Log.e(“,”filesFol:“+filesFol”);
}
静态公共布尔删除目录(文件路径){
if(path.exists()){
File[]files=path.listFiles();
if(files==null){
返回true;
}
对于(int i=0;i目录必须为空才能删除。以下是如何从目录中删除文件,然后删除目录本身的示例
使用以下代码。它应自动删除所有数据。在继续此操作之前,请确保没有数据库/文件或任何缓存资源在使用中:
/**
* Call this method to delete any cache created by app
* @param context context for your application
*/
public static void clearApplicationData(Context context) {
Log.i("delete", "Clearing app cache");
File cache = context.getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
File f = new File(appDir, s);
if(deleteDir(f))
Log.i("delete", String.format("*** DELETED -> (%s) ***",
f.getAbsolutePath()));
}
}
}
private static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
/**
*调用此方法删除应用程序创建的任何缓存
*@param应用程序的上下文
*/
公共静态无效clearApplicationData(上下文){
Log.i(“删除”、“清除应用缓存”);
文件缓存=context.getCacheDir();
File appDir=新文件(cache.getParent());
if(appDir.exists()){
String[]children=appDir.list();
for(字符串s:子项){
文件f=新文件(appDir,s);
if(deleteDir(f))
Log.i(“删除”,String.format(“***已删除->(%s)***”),
f、 getAbsolutePath());
}
}
}
私有静态布尔deleteDir(文件目录){
if(dir!=null&&dir.isDirectory()){
String[]children=dir.list();
for(int i=0;i
如果您查看我的删除目录()
function我使用的是相同的方法。因此,您是……为什么不记录您尝试删除的目录及其内容。然后在删除文件时记录这些文件,以确保该方法正常工作,并确定您尝试删除时目录是否为空。实际上,我正在这样做,尝试删除时返回falseo删除它们,不总是,但有时。我不明白为什么它不起作用,因为我没有做任何特殊的事情。然后发布它尝试删除文件的顺序的输出以及失败的地方