Issue
How to increment filename if the file already exists? Here's the code that I am using -
int num = 0;
String save = at.getText().toString() + ".jpg";
File file = new File(myDir, save);
if (file.exists()) {
save = at.getText().toString() + num +".jpg";
file = new File(myDir, save);
num++;
}
This code works but only 2 files are saved like file.jpg and file2.jpg
Solution
This problem is always initializative num = 0
so if file
exists, it save file0.jpg
and not check whether file0.jpg
is exists ?
So, To code work. You should check until available :
int num = 0;
String save = at.getText().toString() + ".jpg";
File file = new File(myDir, save);
while(file.exists()) {
save = at.getText().toString() + (num++) +".jpg";
file = new File(myDir, save);
}
Answered By - nartoan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.