Android外部存储目录删除

时间:2012-01-30 07:30:33

标签: java android

我有android应用程序,其中包含每日数据库备份工具&包含清晰的设施。我保存了像这样的备份数据

 DBBackup -- dir
    2012-01-25 -dir
        userventura.db  -file
    2012-01-25 -dir
        userventura.db  -file

就像我拯救了一样。

问题是删除目录。例如,如果用户选择日期2012-01-28 this,则需要删除以前的目录。换句话说,与用户选择日期&目录日期,然后它将删除这些文件。

我的问题是我无法删除目录。删除文件userventura.db

    private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
      private final ProgressDialog dialog = new ProgressDialog(getDialogContext());
      String strDate = disdbClrDate.getText().toString();

      protected void onPreExecute() {
         this.dialog.setMessage("MicroSD card Clear..");
         this.dialog.show();
      }

      protected Boolean doInBackground(final String... args) {
         File exportDir = new File(Environment.getExternalStorageDirectory(), "databaseBaup/");

         if (!exportDir.exists()) {
            exportDir.mkdirs();
         }

         try {
             String[] sample = exportDir.list();
             for(int i =0;i < sample.length;i++){
                 File dirc = new File(exportDir, sample[i]);
                 File file = new File(exportDir, sample[i]+"/userventura.jpeg");

                 if(dirc.getName().compareTo(strDate)<=0){
                    try {
                         boolean st = file.delete();

                         boolean dirStatus = dirc.delete();
                         System.out.println("===st=="+st);
                         System.out.println("===dirStatus=="+dirStatus);

                         if(dirc.isDirectory()){

                             boolean bs = file.delete();
                             System.out.println("==bs=="+bs);

                             boolean dircStatus = dirc.delete();
                             System.out.println("===dircStatus=="+dircStatus);
                         }
                    }catch (SecurityException se) {
                        se.printStackTrace();
                    }catch (Exception  e) {
                        e.printStackTrace();
                    }
                 }
             }


            return true;
         } catch (Exception e) {
             e.printStackTrace();
            return false;
         }
      }

      protected void onPostExecute(final Boolean success) {
         if (this.dialog.isShowing()) {
            this.dialog.dismiss();
         }
         if (success) {
            Toast.makeText(DBClearActivity.this, "Micro SD card has been clear data successful!", Toast.LENGTH_SHORT).show();
         } else {
            Toast.makeText(DBClearActivity.this, "Clear data failed", Toast.LENGTH_SHORT).show();
         }

         Intent i = new Intent(getBaseContext(), DbBackupMainActivity.class);
         View vi = SettingActivityGroup.group.getLocalActivityManager().startActivity("SettingScreenActivity", i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
         SettingActivityGroup.group.replaceView(vi);
      }



   }

dirStatus始终返回false,但此行返回true boolean st = file.delete();

请帮帮我......

3 个答案:

答案 0 :(得分:4)

好的,使用此代码删除目录,让我知道发生了什么,

此处路径是您要删除的directory file

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

答案 1 :(得分:1)

如果您要删除目录,请确保Directory is Empty。因此,首先删除您的文件,然后尝试删除目录。在您的代码中,您尝试删除目录两次。所以,只有你应该这样做,

if(dirc.getName().compareTo(strDate)<=0){
                    try {
                          if(dirc.isDirectory()){

                             boolean bs = file.delete();
                             System.out.println("==bs=="+bs);

                             boolean dircStatus = dirc.delete();
                             System.out.println("===dircStatus=="+dircStatus);
                         }
                    }catch (SecurityException se) {
                        se.printStackTrace();
                    }catch (Exception  e) {
                        e.printStackTrace();
                    }

答案 2 :(得分:0)

考虑使用Android&#39; DevicePolicyManager

您的代码中只需要一行,如下所示:

devicePolicyManager.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);

不要忘记将此添加到您的清单中:

   <receiver
        android:name=".WipeDevice" 
        android:description="@string/device_admin_enabled"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
        <meta-data 
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin"/>
    </receiver>