kcuf up February 2016

How to delete file from storage

I have a content menu where it is pop up a menu of rename and delete when you press the item in few seconds. But i dont know how to get the correct directory of one file. Here is my code:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId()) {
        case R.id.rename:
            // edit stuff here
            return true;
        case R.id.delete:

            File dir = new File(Environment.getExternalStorageDirectory()+"/Music/MusicPlayer");
            if (dir.isDirectory())
            {
                String[] children = dir.list();
                for (int i = 0; i < children.length; i++)
                {
                    new File(dir, children[i]).delete();
                }
            }

            // remove stuff here
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

Answers


Gabriel Terry February 2016

Instead of use String[] children = dir.list() try to use:

File dir = new File(PATH);
File[] children = dir.listFiles();
for(int i = 0; i < children.length; i++) {
   children[i].delete();
}


Dhawal Sodha Parmar February 2016

use below function to delete file from Folder, just pass folder path in perameter like

File fDir = new File(PERENT_PATH);
DeleteRecursive(fDir);
// where PERENT_PATH = Environment.getExternalStorageDirectory()+"/folderName"

public static void DeleteRecursive(File fileOrDirectory) {
        if (fileOrDirectory.isDirectory())
            for (File child : fileOrDirectory.listFiles())
                DeleteRecursive(child);

        if (fileOrDirectory.exists()) {
            boolean b = fileOrDirectory.delete();
            if (b) {
                Log.e( "delete dir", "delete dir");
            } else {
                Log.e( "not delete dir", "not delete dir");
            }
        }
    }


Nithinjith February 2016

Please check the Below code

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch(item.getItemId()) {
     .........
     case R.id.delete:
        deleteDirectory(Environment.getExternalStorageDirectory()+"/Music/MusicPlayer");
      .........

    }
    }


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


Dhaval Patel February 2016

Use below util function to either delete File or Directory.

public static boolean delete(File path) {
    boolean result = true;
    if (path.exists()) {
        if (path.isDirectory()) {
            for (File child : path.listFiles()) {
                result &= delete(child);
            }
            result &= path.delete(); // Delete empty directory.
        } else if (path.isFile()) {
            result &= path.delete();
        }
        return result;
    } else {
        return false;
    }
}

Usage:

File dir = new File(Environment.getExternalStorageDirectory()+"/Music/MusicPlayer");
delete(dir);          

Post Status

Asked in February 2016
Viewed 2,760 times
Voted 13
Answered 4 times

Search




Leave an answer