1

I have android application which contain daily database backup facility & contain clear facility.I saved backupdatabse like this

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

Like wise i saved.

Problem is deleting directory. For example if user select the date 2012-01-28 this , then it need to remove previous directory . In otherwords compare with user select date & directory date, then it will delete those files.

My problem is I couldn't remove the Directory.It removing the file 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 always return false, but this line return true boolean st = file.delete();

Please help me out...

Piraba
  • 6,974
  • 17
  • 85
  • 135

3 Answers3

4

Ok, Use this code to remove directory and let me know what happen,

Here path is the directory file for which you want to delete.

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() );
      }
user370305
  • 108,599
  • 23
  • 164
  • 151
1

If you want to delete the Directory just make sure your Directory is Empty. So, first delete your File and then try to Delete the Directory. In your code you are trying to delete the Directory two times. So, only this should do in your case,

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();
                    }
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
0

Consider using Android's DevicePolicyManager

Only requires one line in your code shown below:

devicePolicyManager.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);

Don't forget to add this to your manifest:

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