删除数据库文件夹内部存储 [英] Delete databases folder internal storage
问题描述
我想我的应用程序创建一个应用程序复位功能和我试图删除内部存储包括文件夹中的所有文件
,数据库文件夹
和共享preferences
。问题是,不是每次当我试图删除这些文件夹,它们将被删除。有时候,我用它来删除的文件的函数返回false。在这种情况下,我的应用程序无法正常工作。下面是我使用的是什么:
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 folder
and 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() );
}
有我丢失的东西,我怎么能实现像清除数据
在Android设置功能,将删除数据库了。
Is there something I'm missing and how can I implement function like Clear Data
in Android Settings which will delete the databases too.
推荐答案
使用以下code。它应该会自动删除所有数据。确保没有数据库/文件或任何高速缓存的资源在使用中与此操作前:
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();
}
这篇关于删除数据库文件夹内部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!