How to rename File From filepath in Android?

Nir*_*bhi 3 android rename file path

I have Path Which is stored into one variable That is

String Path:

String filepath="/mnt/sdcard/DCIM/Camera/1396854069062.jpg";

Now i want to rename only file that is 1396854069062.jpg I Reilly don't have any idea for how to do that.

My Code Is:

File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard, filePath);
File to = new File(sdcard, "RChat_Rename.jpg";
from.renameTo(to);
Run Code Online (Sandbox Code Playgroud)

Any help will b appreciated.

Thank You

Inn*_*ler 5

Try this way,

File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"/1396854069062.jpg");
File to = new File(sdcard,"test.jpg");
from.renameTo(to);
Run Code Online (Sandbox Code Playgroud)

Do not forget to add below permission in android manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

Edit

String filepath= Environment.getExternalStorageDirectory() + "/DCIM/Camera/";
File from = new File(filepath,"1396854069062.jpg");
File to = new File(filepath,"test.jpg");
from.renameTo(to);
Run Code Online (Sandbox Code Playgroud)