3

I am making an app in which I have to delete the recently added mp3 file in sdcard. The format in which the song is saved is:

Songhello_17_26.amr

where 17_26 is the time when song was added. Can anyone help me how delete the recently added file in sdcard. I mean to say that I want to delete the time means the latest added file should get deleted. Any help will be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

4 Answers4

5

As it states here, you cant do that directly, you first need to get list of files File.listFiles(), Comparator,File.lastModified(), Arrays.sort() and delete.

Edited:

File f = new File(path);

File [] files = f.listFiles();

Arrays.sort( files, new Comparator()
{
    public int compare(Object o1, Object o2) {

        if (((File)o1).lastModified() > ((File)o2).lastModified()) {
            return -1;
        } else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
            return +1;
        } else {
            return 0;
        }
    }

}); 

To delete latest one:

 files[0].delete();
Community
  • 1
  • 1
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • Your link was very good but can you please help me with proper sample code.Your help will be greatly appreciated –  Apr 17 '12 at 12:26
  • With the help of your code i am able to get list of all mp3 files now can you please tell me how to delete latest entry –  Apr 17 '12 at 12:55
  • Hotveryspicy , i am also stuck at same problem and i am also eagerly waiting for your answere means with the help of your code i got all files from sdcard now how to delete the latest entry? –  Apr 17 '12 at 12:56
  • File file = new File(files[o]); file.delete(); – Mohammed Azharuddin Shaikh Apr 17 '12 at 13:05
  • Can you please edit your new answere File file = new File(files[o]); file.delete(); in above answere –  Apr 17 '12 at 13:19
  • you can see, if its not delete the latest file then sort collection reverse order and then delete. – Mohammed Azharuddin Shaikh Apr 17 '12 at 13:23
  • I have made file name hello as updated in your answere now can you please guide me how to get first element in that file –  Apr 17 '12 at 13:45
  • 1
    you can do like this files[0].delete(); directly. – Mohammed Azharuddin Shaikh Apr 17 '12 at 15:42
  • As this answer is also mentioned in another question I've contributed allow me to be critic: Do you really think it's necessary to sort the Array to find the most recent file? http://stackoverflow.com/q/11482771/1063730 I believe traversing through the array once and just holding the most recent item should be more efficient, prove me wrong? :) – nuala Jul 25 '12 at 14:30
2

Try this

  public String[] getDirectoryList(String path) {
     String[] dirListing = null;
     File dir = new File(path);
     dirListing = dir.list();

     Arrays.sort(dirListing, 0, dirListing.length);
     return dirListing;
  }

  String[]  lstFile = getDirectoryList()
 if(lstFile.length > 0){
    File file = new File(lstFile[0]);
    boolean fStatus = file.delete();

  }
Piraba
  • 6,974
  • 17
  • 85
  • 135
  • I just gave this as example but i have dynamic files and i dont know the name , i want to delete file by time means latest entry should get deleted –  Apr 17 '12 at 12:21
  • I accept @hotveryspicy suggestion . `File.listFiles(), Comparator,File.lastModified(), Arrays.sort()` this have to do – Piraba Apr 17 '12 at 12:35
2

**try this method :

public static boolean deleteDirectory(File path) {
    // TODO Auto-generated method stub
    if( path.exists() ) {
        File[] files = path.listFiles();
        for(int i=0; i<files.length; i++) {
    enter code here        if(files[i].isDirectory()) {
                deleteDirectory(files[i]);
            }
            else {
                files[i].delete();
            }
        }
    }
    return(path.delete());
}

or you can use the following code to delete the file from sd-card:

File folder = Environment.getExternalStorageDirectory(); String fileName = folder.getPath() + "/pass/hello.pdf"; with

 String fileName = Environment.getExternalStorageDirectory() + "/pass/hello.pdf";**
Ganesh
  • 662
  • 3
  • 8
  • 26
0

try this too:

  String root_sd = Environment.getExternalStorageDirectory().toString();
 File file = new File(path) ;       
  File list[] = file.listFiles();
    for(File f:list)
      {
     name =  file.getName();
    filestv.setText(f.getName());
    //add new files name in the list
   //  delete.setText(name );

that code you can see the latest file saved in sdcard, i suggest you to follow along this tutorial.

dondondon
  • 881
  • 8
  • 4