2016-06-20 17 views
1

私のアプリは、SDカード上のデータベースを格納しAndroidの削除データベースProgramitically-:android.system.ErrnoException:chmodコマンドは失敗しました:EPERM(操作許可されていない)

DatabaseHandlerクラスを

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にはありませんユーザーがAppをアンインストールすると削除されます(その理由は、そのストアがExternalStorageDirectoryに格納されているためです)。私はユーザーが再びアプリケーションをインストールするときにDBの新しいコピーを作成したい。

したがって、このコードを使用してアプリで最初に実行して以前のデータベースを削除してください。

上checkReinstallApp()メソッドのonCreate

しかし、私はそれを削除得ていない無error.Butデータベースを示さないアプリをアンインストールし、再びそれを再呼び出し

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

    } 

} 

イム。これには解決策を提案してください。

PS-このコードを使用して、DBファイルやその他のファイルを含むフォルダを削除しようとしました。 android.system.ErrnoException:

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

その後そのは、この警告メッセージ

を示すchmodコマンドは失敗しました:EPERM(操作許可されていない)

答えて

0

あなたがアンドロイド6.0.Soを使用しているかもしれませ実行時の許可を考慮する必要があります。

瞬時の解決策:gradleのtargetSdkVersionを22に変更してください。推奨されていませんが動作します。 Runtime permission EACCES error

+0

はい、私はcode..pleaseチェックWebViewActivity.this.deleteDatabase(SET_START_DIRECTORY + "MYDB")上でやっていることのthats;:

このリンクを参照してください。 – san88

+0

さて、ファイルdbFile = getDatabasePath(SET_START_DIRECTORY + "MyDb"); ブール値is_deleted = dbFile.delete(); –

+0

私もチェックしています.itsはfalseを返しますが、dbFile.exists()はtrueを返します – san88

関連する問題