简体   繁体   中英

renameTo not working for renaming files

I am trying to rename an image file in my app . But the code is not working .
Basically, I want user to click on Rename button , pop up an alert dialog with an editText, user enters the new name of the image , and selects Rename option. This should rename the file but it does nothing. Also I am not getting any errors or exceptions. But a warning, Result of 'File.renameTo()' is ignored.
How do I fix it ?

buttonRename.setOnClickListener(
                        new View.OnClickListener(){
                            public void onClick(View view){
                                AlertDialog.Builder builder2 = new AlertDialog.Builder(PhotosActivity.this);
                                builder2.setMessage("Rename File");
                                final EditText input = new EditText(PhotosActivity.this);
                                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                        LinearLayout.LayoutParams.MATCH_PARENT,
                                        LinearLayout.LayoutParams.MATCH_PARENT);
                                input.setLayoutParams(lp);
                                builder2.setView(input);
                                builder2.setPositiveButton(
                                        "Rename",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {

                                                File oldName =new File(al_images.get(int_position).getAl_imagepath().get(position));
                                                String string = input.getText().toString();
                                                boolean success = oldName.renameTo(new File(string));
                                                if(!success){
                                                    Log.v(TAG,"not renamed");
                                                }

                                            }
                                        });

                                builder2.setNegativeButton(
                                        "Cancel",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                dialog.cancel();
                                            }
                                        });

                                AlertDialog alert12 = builder2.create();
                                alert12.show();

                            }
                        }
                );

You should first check that the file exists before trying to rename it because : File.renameTo() doesn't throw any exception even if the object target of the invocation refers a not existing path.

if (!originalFile.exists()){
   throw new RuntimeException("file " + originalFile + " should exist");
}

And a better alternative is using the java.nio API to manipulate files as it provides same features as File and more and also perform them generally nicer.

You could write :

Files.move(originalPath, targetPath);

Now, if originalPath doesn't refer an existing path, an exception will be thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM