In my Android application, I want to rename the file name at runtime. How can I do it?
This is my code:
String[] command = {" mv", "sun moon.jpg"," sun_moon,jpg"};
try
{
Process process = Runtime.getRuntime().exec(command);
}
catch (IOException e)
{
Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();
}
I also used renameTo(File f) method but it does not work.
I would recommend using
File.renameTo()
rather than running themv
command, since I'm fairly sure the latter isn't supported..Have you given your application permission to write to the SD Card?
You do this by adding the following to your
AndroidManifest.xml
:If it doesn't work once the permission is added check the device log for errors when you try to rename the file (either using the
adb
command or in the logcat view in Eclipse).When accessing the SD Card you shouldn't hard-code the path but instead use the
Environment.getExternalStorageDirectory()
method to get the directory.The following code works for me:
and if you want to check the process, you can do like:
I tried adding permissions. Even though it did not work, adding
File1.setWritable(true);
enabled me to rename the file.Below is my code snippet:
you can also explicitly give the full path without specifying directory...