机器人,如何重命名文件? [英] android, How to rename a file?

查看:169
本文介绍了机器人,如何重命名文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我需要录制视频。录制开始之前,在我指定一个名称和目录到它。录制结束后用户必须重新命名其文件的能力。我写了下面的code,但似乎这是行不通的。

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 file. I wrote following code but seem it doesn't work.

当用户输入文件名,然后单击按钮,我会做到这一点:

When user enters name of file and click on button I'll do this:

private void setFileName(String text) {     
        String currentFileName = videoURI.substring(videoURI.lastIndexOf("/"), videoURI.length());
        currentFileName = currentFileName.substring(1);
        Log.i("Current file name", currentFileName);

        File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), MEDIA_NAME);
        File from      = new File(directory, "currentFileName");
        File to        = new File(directory, text.trim() + ".mp4");
        from.renameTo(to);
        Log.i("Directory is", directory.toString());
        Log.i("Default path is", videoURI.toString());
        Log.i("From path is", from.toString());
        Log.i("To path is", to.toString());
    }

文本:是由用户输入的名称。 当前文件名:是由我在录制前指定的名称 Media_name表:文件夹的名称

Text: is the name which is entered by user. Current Filename: is the name which is assigned by me before recording MEDIA_NAME: name of folder

LogCat中显示了这一点:

Logcat shows this:

05-03 11:56:37.295: I/Current file name(12866): Mania-Karaoke_20120503_115528.mp4
05-03 11:56:37.295: I/Directory is(12866): /mnt/sdcard/Movies/Mania-Karaoke
05-03 11:56:37.295: I/Default path is(12866): /mnt/sdcard/Movies/Mania-Karaoke/Mania-Karaoke_20120503_115528.mp4
05-03 11:56:37.295: I/From path is(12866): /mnt/sdcard/Movies/Mania-Karaoke/currentFileName
05-03 11:56:37.295: I/To path is(12866): /mnt/sdcard/Movies/Mania-Karaoke/hesam.mp4

任何建议将AP preciated。

Any suggestion would be appreciated.

推荐答案

现在的问题是在这条线,

The problem is in this line,

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

下面 currentFileName 实际上你没有一个String必须使用

Here currentFileName is actually a String you dont have to use "

尝试这种方式,

File from      = new File(directory, currentFileName  );
                                    ^               ^         //You dont need quotes

这篇关于机器人,如何重命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆