Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android, How to rename a file?

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

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

Any suggestion would be appreciated.

like image 807
Hesam Avatar asked May 03 '12 04:05

Hesam


People also ask

Why I Cannot rename a file in Android?

If the option to rename a file is grayed out, it means that you have no permission to change it. Technically, these files are protected files, and if you rename them, your Android device will become unstable.

How do you rename photos on Android?

To change the name from any of them, we just have to go to the image we want, make a long press and touch Rename. From there we write the name that seems good to us and OK.

Can you rename a file in Android Studio?

Open the settings. gradle file with a text editor, like VSCode, and change the rootProject.name to your new project name. Done!


2 Answers

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

like image 118
NT_ Avatar answered Oct 02 '22 12:10

NT_


The problem is in this line,

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

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

try it this way,

File from      = new File(directory, currentFileName  );                                     ^               ^         //You dont need quotes 
like image 44
COD3BOY Avatar answered Oct 02 '22 12:10

COD3BOY