如何在Android中从文件路径重命名文件? [英] How to rename File From filepath in Android?
本文介绍了如何在Android中从文件路径重命名文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的路径存储在一个变量中
I have Path Which is stored into one variable That is
String filepath ="/mnt/sdcard/DCIM/Camera/1396854069062.jpg";
String filepath="/mnt/sdcard/DCIM/Camera/1396854069062.jpg";
现在,我只想重命名为 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.
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard, filePath);
File to = new File(sdcard, "RChat_Rename.jpg";
from.renameTo(to);
任何帮助都会被感激.
谢谢
推荐答案
尝试这种方式,
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"/1396854069062.jpg");
File to = new File(sdcard,"test.jpg");
from.renameTo(to);
别忘了在Android清单文件中添加以下权限
Do not forget to add below permission in android manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
修改
String filepath= Environment.getExternalStorageDirectory() + "/DCIM/Camera/";
File from = new File(filepath,"1396854069062.jpg");
File to = new File(filepath,"test.jpg");
from.renameTo(to);
这篇关于如何在Android中从文件路径重命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文