How to delete the last file from any specific folder in sdcard in Android?

I am creating a camera application. Thanks to this, I can capture the image and save it in a specific folder. But during this process, the image becomes saved in the default position: sdcard / DCIM / CAMERA .

The file name is the current date. When the user clicks the save button, then the current date is executed and a file name is created. So I can not determine the file name.

can someone suggest me how to access the file name or how to delete the last image file programmatically.

0
source share
2 answers

, - . ; .

File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"from.jpg");
File to = new File(sdcard,"to.jpg");
from.renameTo(to);
0

, , File . :

public static File getLastModifiedFile( File[] allFiles ) [
    File result = allFiles[1];
    //you cou also use result = null and uncomment the
    //other if-statement. It probably the first idea but in that case you might
    //return null while here you'll get an exception which makes debugging easier :)

    for( int i = 0; i < allFiles.length; i++ ) {
        if( result.lastModified() < allFiles[i].lastModified() ) {
        //if( result == null || result.lastModified() < allFiles[i].lastModified() ) {
            result = currentFile;
        }
    ]
    return result;
}

. lastModified().

0

All Articles