2011-11-29 82 views
1

我试图为我的应用程序创建一个重置应用程序功能,我试图从内部存储器中删除所有文件,包括files folderdatabases foldershared preferences。问题是,不是每次我尝试删除这些文件夹时,它们都被删除。有时我用来删除文件的函数返回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()+"https://stackoverflow.com/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()); 
} 

有我丢失的东西和我如何能实现像在Android设置Clear Data这将删除数据库过于功能。

回答

1

一个目录必须是空的才能被删除。以下是如何从目录中删除文件然后删除目录本身的示例。

http://www.rgagnon.com/javadetails/java-0483.html

+0

如果您看看我的'deleteDirectory()'函数,我使用的是同样的方法。 –

+1

所以你是...为什么你不记录你想删除的目录,它的内容。然后在删除文件时记录这些文件,以确保该方法正在运行,并确定当您试图删除它时该目录是否实际为空。 – Craigy

+0

其实我正在做这件事,当我试图删除它们时,它会返回false,但并非总是有时。而且我无法弄清楚为什么它不起作用,因为我没有做任何特别的事情。 –

2

使用下面的代码。它应该自动删除所有数据。在继续之前,请确保没有数据库/文件或任何缓存资源在使用中:

/** 
* 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(); 
}