如何使用 Android 应用程序重命名 sdcard 上的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2896733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to rename a file on sdcard with Android application?
提问by Addy
In my Android application, I want to rename the file name at runtime. How can I do it?
在我的 Android 应用程序中,我想在运行时重命名文件名。我该怎么做?
This is my code:
这是我的代码:
String[] command = {" mv", "sun moon.jpg"," sun_moon,jpg"};
try
{
Process process = Runtime.getRuntime().exec(command);
}
catch (IOException e)
{
Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();
}
I also used renameTo(File f) method but it does not work.
我也使用了 renameTo(File f) 方法,但它不起作用。
回答by Dave Webb
I would recommend using File.renameTo()
rather than running the mv
command, since I'm fairly sure the latter isn't supported..
我建议使用File.renameTo()
而不是运行mv
命令,因为我很确定后者不受支持..
Have you given your application permission to write to the SD Card?
您是否已授予您的应用程序写入 SD 卡的权限?
You do this by adding the following to your AndroidManifest.xml
:
您可以通过将以下内容添加到您的AndroidManifest.xml
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If it doesn't work once the permission is added check the device log for errors when you try to rename the file (either using the adb
command or in the logcatview in Eclipse).
如果添加权限后它不起作用,请在尝试重命名文件时检查设备日志是否有错误(使用adb
命令或在 Eclipse的logcat视图中)。
When accessing the SD Card you shouldn't hard-code the path but instead use the Environment.getExternalStorageDirectory()
method to get the directory.
访问 SD 卡时,您不应该对路径进行硬编码,而是使用该Environment.getExternalStorageDirectory()
方法来获取目录。
The following code works for me:
以下代码对我有用:
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"from.txt");
File to = new File(sdcard,"to.txt");
from.renameTo(to);
and if you want to check the process, you can do like:
如果你想检查这个过程,你可以这样做:
boolean renamed = from.renameTo(to);
if (renamed) {
Log.d("LOG","File renamed...");
}else {
Log.d("LOG","File not renamed...");
}
回答by Jawad Zeb
you can also explicitly give the full path without specifying directory...
您还可以在不指定目录的情况下显式提供完整路径...
File file = new File("Path of file which you want to rename");
File file2 = new File("new name for the file");
boolean success = file.renameTo(file2);
回答by ashish bulchandani
I tried adding permissions. Even though it did not work, adding File1.setWritable(true);
enabled me to rename the file.
我尝试添加权限。即使它不起作用,添加File1.setWritable(true);
使我能够重命名文件。
Below is my code snippet:
下面是我的代码片段:
if(from.setWritable(true))
Log.d("InsertFragmentTwo ", "FileName==> Is Writable");
File two = new File(sdcard,""+imageCount+"."+s.substring((s.lastIndexOf(".")+1)));
if (from.renameTo(two)) {
Log.d("InsertFragmentTwo ", "New FileName==> " + temp);
imageCount++;
retrofitImageUpload(temp);
} else
Log.d("InsertFragmentTwo ", "File Renaming Failed");
回答by Tayyab Hayat
public void selectFile() {
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems = {
"Select file from internal storage"};
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
choosePhotoFromGallary();
break;
}
}
});
pictureDialog.show();
}
public void choosePhotoFromGallary() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
return;
}
if (requestCode == GALLERY) {
if (data != null) {
Uri contentURI = data.getData();
File dir = Environment.getExternalStorageDirectory();
if(dir.exists()){
File from = new File(dir, String.valueOf(GALLERY));
File to = new File(dir,"filerename.txt");
if(from.exists())
from.renameTo(to);
}
}
}
}