2016-06-20 25 views
1

我的應用程序商店SD卡上的數據庫的Android刪除數據庫Programitically-:android.system.ErrnoException命令:chmod失敗:EPERM(不允許的操作)

數據庫處理器類

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

所以我的DB不在用戶卸載應用程序時刪除(因爲其存儲位於ExternalStorageDirectory下)。我想在用戶再次安裝應用程序時創建數據庫的新副本。

因此,即時檢查第一次運行應用程序,並通過使用此代碼刪除以前的數據庫。

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"); 

    } 

} 

林調用checkReinstallApp()方法對我

的onCreate

但是,當我卸載應用程序,並重新安裝它,它沒有表現出error.But數據庫沒有得到刪除。請爲此提出解決方案。

PS-I嘗試使用此代碼刪除包含DB文件和其他文件的文件夾。

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; 
} 

那麼它顯示此警告消息

:android.system.ErrnoException命令:chmod失敗:EPERM(不允許的操作)

回答

0

可能是您的Android 6.0.So你必須考慮運行時權限。

即時解決方案:將gradle中的targetSdkVersion更改爲22.不推薦,但有效。

請參考以下鏈接:Runtime permission EACCES error

+0

是多數民衆贊成我對code..please檢查WebViewActivity.this.deleteDatabase(SET_START_DIRECTORY + 「MYDB」)完成的; – san88

+0

好吧,File dbFile = getDatabasePath(SET_START_DIRECTORY +「MyDb」); boolean is_deleted = dbFile.delete(); –

+0

我檢查過.its返回false.But dbFile.exists()返回true – san88

相關問題