Android : can't rename all files in a folder

on Monday, October 13, 2014


I have some pictures in a folder on sdcard with these names:



000.jpg
001.jpg
002.jpg
003.jpg
004.jpg
...


the sort of names is so important and should always be like this. Now I want to keep this sort even after deleting a photo. I mean if I deleted for example the third photo, then my fourth photo should be renamed to 002, fifth to 003 and so on.


I writed this code for my purpose:



// "myfiles" is a File array of directory listfiles.
// "str1" is my folder name and I checked it for existences before
// "numb" is the pic number that should delete.
File pic = new File(Environment.getExternalStorageDirectory() + "/Albummaker/" + str1 + "/" + String.format("%03d", numb) + ".jpg");
pic.delete();

for (int i = numb; i < myfiles.length; i++) {
File f = new File(Environment.getExternalStorageDirectory() + "/Albummaker/" + str1 + "/"+myfiles[i].getName());
f.renameTo(new File(Environment.getExternalStorageDirectory() + "/Albummaker/" + str1 + "/"+String.format("%03d", i)+".jpg"));
}


but when I run this it deletes the photos before the deleted one (In this case 001 and 002) and then rename rest of them (and I'll have 3 photos after this (002, 003, 004)).


any idea?


0 comments:

Post a Comment