0

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

  1. How to rename File From filepath in Android?
  2. How to rename a file on sdcard with Android application?
  3. How to rename a file in internal Storage?

Why am not able to rename file, Or what wrong am doing ?

sanoj lawrence
  • 951
  • 5
  • 29
  • 69

1 Answers1

0

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

and uninstalled app, and then reinstalled.

and i was able to rename file, And later i found i didn't enable write permission on my mobile device.

sanoj lawrence
  • 951
  • 5
  • 29
  • 69