Am trying to implement a file rename option so i tried
confirmEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
renameFile(filePath, editFileName.getText().toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public void renameFile(File toBeRenamed, String new_name)
throws IOException {
//need to be in the same path
File fileWithNewName = new File(toBeRenamed.getParent(), new_name);
if (fileWithNewName.exists()) {
throw new IOException("file exists");
}
// Rename file (or directory)
boolean success = toBeRenamed.renameTo(fileWithNewName);
if (!success) {
// File was not successfully renamed
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
});
and i get Not success i also added <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and followed android, How to rename a file? and tried couple of answer and all fails. And even i have tried multiple topics like below
- How to rename File From filepath in Android?
- How to rename a file on sdcard with Android application?
- How to rename a file in internal Storage?
Why am not able to rename file, Or what wrong am doing ?