Basheer
Basheer

Reputation: 404

How to rename the file from external storage (Sdcard)

I have been trying to rename the file from external storage (Sdcard) using below code.But not rename the file, boolean return the false value.This code working fine for internal storage (Phone memory)

File f1= new File(sdcard_current_path); File f2= new File(sdcard_rename_path); boolean isSuccess=f1.renameTo(f2);

Upvotes: 1

Views: 2485

Answers (3)

Aman Saxena
Aman Saxena

Reputation: 1919

To use function renameTo() your source and destination should be on same mount point see android doc https://developer.android.com/reference/java/io/File#renameTo(java.io.File)

enter image description here

Hope it will help you to resolve the issue.

Upvotes: 0

Vikram Kaldoke
Vikram Kaldoke

Reputation: 150

Make sure is directory present in storage. you can use the following code

File f1= new File(sdcard_current_path, filename);
File f2= new File(sdcard_rename_path, newfilname); 
boolean isSuccess=f1.renameTo(f2);

Upvotes: 1

Hossein Seifi
Hossein Seifi

Reputation: 1420

First add this line to AndroidManifest.xml:

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

Then use your code.

Upvotes: 0

Related Questions