My App stores its database on SD card
DatabaseHandler class
private static final String DATABASE_NAME = "MyDb";
private static final String KEY_ROOT_FOLDER= Environment.getExternalStorageDirectory().getAbsolutePath() + "/.MYAPP/" + DATABASE_NAME;
public DatabaseHandler(Context context) {
Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+ FOLDER_NAME+File.separator, null, DATABASE_VERSION);
super(context, KEY_ROOT_FOLDER, null, DATABASE_VERSION);
}
So my DB does not delete when user uninstalls the app (because its stores under ExternalStorageDirectory). I want to create fresh copy of the DB when user installing app again.
Therefore im checking first time run on the app and delete previous database by using this code.
public void checkReinstallApp(){
SharedPreferences settings = getSharedPreferences("FIRSTRUN", 0); // Get preferences file (0 = no option flags set)
boolean firstRun = settings.getBoolean("firstRun", true); // Is it first run? If not specified, use "true"
if (firstRun) {
Log.w("activity", "first time");
isFirstTime=true;
if(permissionHelper.isPermissionGranted( Manifest.permission.READ_EXTERNAL_STORAGE)){ //for getting permission Android 6
SharedPreferences.Editor editor = settings.edit(); // Open the editor for our settings
editor.putBoolean("firstRun", false); // It is no longer the first run
editor.commit(); // Save all changed settings
try{
WebViewActivity.this.deleteDatabase(SET_START_DIRECTORY+"MyDb");
}catch (Exception e){
// Error
}
File f = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/.MYAPP/");
deleteFile(f);
}else{
permissionHelper.setForceAccepting(true).request(PERMISSION);
}
} else {
Log.w("activity", "second time");
}
}
Im calling checkReinstallApp() method on my
onCreate
But when i uninstalls the app and reinstall it again it showing no error.But database not getting delete. Please suggest solution for this.
PS-I have tried to delete the folder containing DB file and other files by using this code.
File f = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/.MYAPP/");
deleteFile(f);
public boolean deleteFile(File file) {
if(permissionHelper.isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (file != null) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteFile(new File(file, children[i]));
if (!success) {
return false;
}
}
}
return file.delete();
}
}else {
permissionHelper.setForceAccepting(true).request(PERMISSION);
deleteFile( file);
}
return false;
}
Then it's showing this warning message
: android.system.ErrnoException: chmod failed: EPERM (Operation not permitted)