【问题标题】:How to rename a file on sdcard with Android application?如何使用 Android 应用程序重命名 sdcard 上的文件?
【发布时间】:2011-02-23 05:09:06
【问题描述】:

在我的 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) 方法,但它不起作用。

【问题讨论】:

标签: android runtime filenames rename


【解决方案1】:

我建议使用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 命令或在 Eclipse 中的 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...");
}

【讨论】:

  • 现在我试试 File f1=new File("/sdcard/sun moon.jpg");文件 f2=new File("/sdcard/soon_moon.jpg");试试 { f1.renameTo(f2); } catch (Exception e) { Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();并在 androidManifest.xml 文件中使用权限 但仍然没有变化。
  • 我已经用适合我的代码更新了我的答案。 renameTo() 运行时是否返回truefalse?如果返回false,那么f1.exists()返回什么?
【解决方案2】:

你也可以在不指定目录的情况下明确给出完整路径...

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

【讨论】:

    【解决方案3】:

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

    下面是我的代码sn-p:

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

    【讨论】:

      【解决方案4】:
      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);
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-09
        • 2014-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多