开发者

Deleting a non-empty folder from sdcard

I am trying to make my application delete a folder on sdcard (with subfolders) on start, but I dont understand how to set the path to be delete.

I created a simple Activity to delete this folder like this:

package org.android.test;    
import java.io.File;    
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class DelSD extends Activity {

File dir = new File(android.os.Environment
        .getExternalStorageDirectory().getPath(),
        "/TEST");

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
    }

    void DeleteRecursive(File dir)
    {
        Log.d("DeleteRecursive", "DELETEPREVIOUS TOP" + dir.getPath());
        if (dir.isDirectory())
        {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) 
            {
               File temp =  new File(dir, children[i]);
               if(temp.isDirectory())
               {
                   Log.d("DeleteRecursive", "Recursive Call" + temp.getPath());
                   DeleteRecursive(temp);
               }
               else
               {
                   Log.d("DeleteRecursive", "Delete File" + temp.getPath());
                   boolean b = temp.delete();
                   if(b == false)
                   {
       开发者_Go百科                Log.d("DeleteRecursive", "DELETE FAIL");
                   }
               }
            }    
            dir.delete();
        }
    }     
}

and added to my manifest:

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"

But still no go. My TEST folder on sdcard still there. I tested this Activity on emulator and on my device 2.2.

I dont get how I should set my path to be deleted with:

File dir = new File(android.os.Environment
            .getExternalStorageDirectory().getPath(),
            "/TEST");

because if I change the "dir" to something else the "void DeleteRecursive(File dir)" does not complain about "dir" not been set! So this is not working at all. Any sugestion?


import java.io.File;

class DeleteDir {
  public static void main(String args[]) {
    deleteDirectory(new File(args[0]));
  }

  static public boolean deleteDirectory(File path) {
    if( path.exists() ) {
      File[] files = path.listFiles();
      for(int i=0; i<files.length; i++) {
         if(files[i].isDirectory()) {
           deleteDirectory(files[i]);
         }
         else {
           files[i].delete();
         }
      }
    }
    return( path.delete() );
  }
}


Try out this:

public void DeleteFromSdCard()                          //Testing purpose only
{
    File checkFile = new File("/sdcard/TEST/");
    File[] lstFile;

    if(checkFile.exists())
    {
        lstFile = checkFile.listFiles();

        for(int i =0; i<lstFile.length;i++)
        {
            File file = lstFile[i];
            file.delete();
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜