Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename File From filepath in Android?

I have Path Which is stored into one variable That is

String Path:

String filepath="/mnt/sdcard/DCIM/Camera/1396854069062.jpg";

Now i want to rename only file that is 1396854069062.jpg I Reilly don't have any idea for how to do that.

My Code Is:

File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard, filePath);
File to = new File(sdcard, "RChat_Rename.jpg";
from.renameTo(to);

Any help will b appreciated.

Thank You

like image 576
Nirav Dabhi Avatar asked Apr 07 '14 11:04

Nirav Dabhi


People also ask

How do I rename a file programmatically?

File from = new File(directory, "currentFileName"); For safety, Use the File. renameTo() .

How do I rename a .file file?

Open File Explorer by going to My Computer, or by pressing Windows Key + E on your keyboard. Find the file you want to rename, select it and select Rename on the ribbon (or press F2 on your keyboard). Type the new name you want the file to have and press Enter.

How do I rename files on my Android phone?

Locate the “My Files” folder on your Android device and tap to launch it. Inside the “My Files” folder, locate the file you’ll rename. Once you locate the file, long-press on it until it is selected. Tap the “More” button, which is shown as a kebab (3-vertical-dot icon) menu. On the menu that appears, select “Rename” by pressing it.

How do I rename a file in a list?

On the bottom, tap Browse . Tap a category or a storage device. You'll see files from that category in a list. Next to a file you want to rename, tap the Down arrow . If you don't see the Down arrow , tap List view .

How do I change the name of a file in Linux?

Inside the “My Files” folder, locate the file you’ll rename. Once you locate the file, long-press on it until it is selected. Tap the “More” button, which is shown as a kebab (3-vertical-dot icon) menu. On the menu that appears, select “Rename” by pressing it. Encode the file’s new name on the space provided.

How do I rename multiple files in X-plore?

Launch the X-plore app and tap on Internal shared storage or select your memory card. Tap on the folder containing the files you want to organize. Select all the files you want to organize. Long-press on any of the selected files and tap Rename. A Batch rename menu should come up. Tap the edit icon on the row labeled Name.


1 Answers

Try this way,

File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"/1396854069062.jpg");
File to = new File(sdcard,"test.jpg");
from.renameTo(to);

Do not forget to add below permission in android manifest file

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

Edit

String filepath= Environment.getExternalStorageDirectory() + "/DCIM/Camera/";
File from = new File(filepath,"1396854069062.jpg");
File to = new File(filepath,"test.jpg");
from.renameTo(to);
like image 133
InnocentKiller Avatar answered Sep 28 '22 14:09

InnocentKiller