Reputation: 8538
A quick question, how do i rename a file?
File to = new File(f.getAbsolutePath(), etRenameStr.getText().toString() );
f.renameTo(to);
expl();
tried like that, but doesn't seem to work.
Thanks!
Upvotes: 0
Views: 330
Reputation: 86
File dir = Environment.getExternalStorageDirectory();
if(dir.exist()){
File from = new File(dir,"from.mp4");
File to = new File(dir,"to.mp4");
if(from.exist())
from.renameTo(to);
}
http://developer.android.com/reference/java/io/File.html#renameTo%28java.io.File%29
Upvotes: 2
Reputation: 21191
File rootDir = Environment.getExternalStorageDirectory();
File file = new File(rootDir + "/Files/"+fileName);
File file2 = new File("newname");
// Rename file (or directory)
boolean success = file.renameTo(file2);
if (!success) {
System.out.println("File was not successfully renamed");
}
This worked for me. please check once!!
Upvotes: 1
Reputation: 2573
I think getAbsolutePath()
returns the full path including file name, that might be a problem here. Try getParent()
instead and see if it works.
Upvotes: 1