Delete databases folder internal storage

可紊 提交于 2019-12-11 13:56:27

问题


I'm trying to create a reset app functionality for my application and I'm trying to delete all files from internal storage including files folder, databases folderand shared preferences. The problem is that not everytime when I try to delete these folder they are deleted. Sometimes the function which I use to delete the files returns false. In that case my application cannot work properly. Here is what I'm using :

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

Is there something I'm missing and how can I implement function like Clear Data in Android Settings which will delete the databases too.


回答1:


Use the following code. It should delete all the data automatically. Make sure no database/file or any cache resource is in-use before proceeding with this:

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



回答2:


A directory must be empty to be deleted. Here is an example of how to delete files from a directory and then delete the directory itself.

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



来源:https://stackoverflow.com/questions/8311751/delete-databases-folder-internal-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!