5

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);

Any help will b appreciated.

Thank You

Nirav Dabhi
  • 341
  • 1
  • 7
  • 29

2 Answers2

6

Try this way,

File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"/1396854069062.jpg");
File to = new File(sdcard,"test.jpg");
from.renameTo(to);

Do not forget to add below permission in android manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Edit

String filepath= Environment.getExternalStorageDirectory() + "/DCIM/Camera/";
File from = new File(filepath,"1396854069062.jpg");
File to = new File(filepath,"test.jpg");
from.renameTo(to);
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
4

Try as below...

File fileDir = Environment.getExternalStorageDirectory() +"/DCIM/Camera/";
File from = new File(fileDir, "1396854069062.jpg");
File to = new File(fileDir, "RChat_Rename.jpg";
from.renameTo(to);
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41