问题
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
回答1:
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);
回答2:
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);
来源:https://stackoverflow.com/questions/22911025/how-to-rename-file-from-filepath-in-android