android, How to rename a file?

前端 未结 9 2120
Happy的楠姐
Happy的楠姐 2020-11-28 09:58

In my application, I need to record video. Before start of recording in I\'m assigning a name and directory to it. After recording is finished user has ability to rename his

9条回答
  •  被撕碎了的回忆
    2020-11-28 10:28

    In your code:

    Shouldn't it be :

    File from = new File(directory, currentFileName);

    instead of

    File from = new File(directory, "currentFileName");


    For safety,

    Use the File.renameTo() . But check for directory existence before renaming it!

    File dir = Environment.getExternalStorageDirectory();
    if(dir.exists()){
        File from = new File(dir,"from.mp4");
        File to = new File(dir,"to.mp4");
         if(from.exists())
            from.renameTo(to);
    }
    

    Refer: http://developer.android.com/reference/java/io/File.html#renameTo%28java.io.File%29

提交回复
热议问题