Android, How to Rename a File

android, How to rename a file?

The problem is in this line,

File from = new File(directory, "currentFileName");

Here currentFileName is actually a String you dont have to use "

try it this way,

File from      = new File(directory, currentFileName  );
^ ^ //You dont need quotes

How to rename a file in internal Storage?

As we discussed in the comments, I believe your renameTo() is not working because of some type of hold on the file, but you did not post your full code so I cannot be completely sure.

With the code you provided, I have created a MainActivity that accomplishes a rename of the file you noted that was stored in Internal Storage (i.e. pippo.png). The successful rename is proved in the debug logs when you run the Activity.

Note: In my solution below I am just creating Files and placing them where you said they are supposed to go to provide you an answer of how renameTo() should/can be used in your app, I am not actually working with images as you did not provide me with your code that you use to access the images. I'm sure you realize this, but you will need to be sure the image you are working with is being selected correctly by the user and the path is accurate for my example to work when you plug it into your real application.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String imageFilename = "pippo.png";
//example username, I am not sure how you get this info
String exampleUsername = "user1";

try {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
//check that we are good here...
if(directory.exists())
Log.d("ImageTAG", "'imageDir' exists");

// create imageDir
File completeImagePath = new File(directory, imageFilename);

//write file
FileOutputStream out = new FileOutputStream(completeImagePath);

out.flush();
out.close();

//check to ensure complete image path exists... it should
if(completeImagePath.exists())
Log.d("ImageTAG", "'completeImagePath' exists");

//show full path on device
Log.d("ImageTAG", "Image saved success, complete Image Path: " +
completeImagePath.getAbsolutePath());

//redeclaration of file here is not needed, but added for clarity
File from = new File(completeImagePath.getAbsolutePath());
//what you are renaming the file to
File to = new File(directory, exampleUsername + ".png");
//now rename
Boolean success = from.renameTo(to);

Log.d("ImageTAG", "Successful Rename: "+success.toString()+"| File is now named: "+to.getPath());

} catch (Exception e) {
e.printStackTrace();
Log.d("ImageTAG","saved failed");
}

}
}

Rename a file in the internal storage

renameTO() doesn't work in my environment (Eclipse Indigo, AVD with android version 2.3). 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.

Android code for file rename process

Make sure you have write permissions defined in the manifest file:

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

The File.renameTo() method will fail under the following conditions:

  • Write permission is required on the directories containing both the
    source and destination paths.
  • Search permission is required for all parents of both paths.
  • Both paths be on the same mount point. On Android, applications are
    most likely to hit this restriction when attempting to copy between
    internal storage and an SD card.

As described in the documentation: http://developer.android.com/reference/java/io/File.html#renameTo(java.io.File)

Additionally, I would verify that the newFile path is what you expect it to be.



Related Topics



Leave a reply



Submit