Android how to rename File in internal storage

I got one image file from Internal Storage, example name of file is name_image_old, i want rename become name_image_new. i have tried use function renameTO() but not working. My code:

  private fun rename(from: File, to: File): Boolean {
        return from.parentFile.exists() && from.exists() && from.renameTo(to)
    }

val currentFile = File("/sdcard/currentFile.txt")
                val newFile = File("/sdcard/newFile.txt")

                if (rename(currentFile, newFile)) {
                    //Success
                    Log.i(TAG, "Success")
                } else {
                    //Fail
                    Log.i(TAG, "Fail")
                }

Upvotes: -1

Views: 1165

Answers (2)

midou
midou

Reputation: 75

An easier way to do this fun File.rename(to:String):Boolean{ return renameTo(File(this.parentFile, to)) }

Upvotes: 0

SAVAN JADAV
SAVAN JADAV

Reputation: 112

xin chào Xuân Đức Nguyễn

renameTO() doesn't work in my environment. The solution is to skip the temporary file method alltogeher, since it doesn't seem to be possible to solve in any reasonable time frame.

so i suggest not use File.renameTo() method in Internal Storage environment. Renaming a file in this environment, can do: - Copy content of the old file to new file. - Delete old file. :)

Upvotes: -1

Related Questions