简体   繁体   中英

Delete last(recent) image from gallery from internal memory

I want to delete image from gallery that was most recently captured. I have followed a couple of SO questions, but nothing seems to work. Can anyone give me a simple code that works?

Sample code:

public void deleteImage() {

    File f = new File("/storage/emulated/0/Pictures");

    File [] files = f.listFiles();

    Arrays.sort(files, new Comparator<File>() {
        @Override
        public int compare(File a, File b) {
            if (a.lastModified() < b.lastModified())
                return 1;
            if (a.lastModified() > b.lastModified())
                return -1;
            return 0;
        }
    });

    files[0].delete();
}

This code sorts the files in gallery folder by time of last modification, and delete the most recent file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM