1

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)

Cœur
  • 37,241
  • 25
  • 195
  • 267
san88
  • 1,286
  • 5
  • 35
  • 60

1 Answers1

0

May be you are using android 6.0.So you have to consider runtime permission.

Instant solution: Change targetSdkVersion in the gradle to 22.Not recommended but works.

Refer this link: Runtime permission EACCES error

Community
  • 1
  • 1
Smit.Satodia
  • 597
  • 4
  • 13
  • yes thats what i have done on the code..please check WebViewActivity.this.deleteDatabase(SET_START_DIRECTORY+"MyDb"); – san88 Jun 20 '16 at 11:21
  • Okay so, File dbFile = getDatabasePath(SET_START_DIRECTORY+"MyDb"); boolean is_deleted = dbFile.delete(); – Smit.Satodia Jun 20 '16 at 11:24
  • i have checked that too .its returns false.But dbFile.exists() returns true – san88 Jun 20 '16 at 11:25
  • Are you sure this block doesn't throw any error? try{ WebViewActivity.this.deleteDatabase(SET_START_DIRECTORY+"MyDb"); }catch (Exception e){ Log.e("Error",e.toString) } – Smit.Satodia Jun 20 '16 at 11:27
  • : android.system.ErrnoException: chmod failed: EPERM (Operation not permitted) – san88 Jun 20 '16 at 11:29
  • May be any of your code access the database file so it is unable to delete use https://developer.android.com/reference/java/io/File.html#deleteOnExit() and force restart the app. – Smit.Satodia Jun 20 '16 at 11:29
  • Working on android6.0? – Smit.Satodia Jun 20 '16 at 11:30
  • debuging on Android 6.This code works fine on other versions. But not in 6 – san88 Jun 20 '16 at 11:31
  • im checking run time permission on the code permissionHelper.isPermissionGranted( Manifest.permission.READ_EXTERNAL_STORAGE)){ //for getting permission Android 6 but still its getting this error. im using permissionHelper class for Android 6 run time permissions – san88 Jun 20 '16 at 11:37
  • Change targetSdkVersion in the gradle to 22 – Smit.Satodia Jun 20 '16 at 11:38
  • is there any solution other than that. If i change targetSdkVersion to 22.it conflict with other libs on the gradle – san88 Jun 20 '16 at 12:09