How to rename File From filepath in Android?

瘦欲@ 提交于 2020-12-03 17:57:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!