如何使用Android应用程序重命名SD卡上的文件?

时间:2010-05-24 11:52:24

标签: android runtime filenames rename

在我的Android应用程序中,我想在运行时重命名文件名。我该怎么办?

这是我的代码:

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

我也使用了renameTo(File f)方法,但它不起作用。

4 个答案:

答案 0 :(得分:85)

我建议使用File.renameTo()而不是运行mv命令,因为我很确定后者不受支持..

您是否已经提供了application permission to write to the SD Card

您可以通过adding the following to your AndroidManifest.xml执行此操作:

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

如果在添加权限后它无效,请在尝试重命名文件时检查设备日志中的错误(使用adb命令或在 logcat 视图中蚀)。

访问SD卡时,不应对路径进行硬编码,而应使用the Environment.getExternalStorageDirectory()方法获取目录。

以下代码适用于我:

File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"from.txt");
File to = new File(sdcard,"to.txt");
from.renameTo(to);

如果你想检查过程,可以这样做:

boolean renamed = from.renameTo(to);

if (renamed) {
  Log.d("LOG","File renamed...");
}else {
  Log.d("LOG","File not renamed...");
}

答案 1 :(得分:5)

你也可以明确地给出完整路径而不指定目录......

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

答案 2 :(得分:0)

我尝试添加权限。即使它不起作用,添加File1.setWritable(true);也可以让我重命名该文件。

以下是我的代码段:

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

答案 3 :(得分:0)

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