java - renameTo() 不适用于重命名图像并返回 false

标签 java android file file-rename

我有一个图片库应用程序,我尝试使用 renameTo() 方法重命名图像,但它无法更改文件名并返回 false。我读了很多关于重命名文件的问题,所有这些问题都建议只使用一种重命名文件的方法 - renameTo()。奇怪的是,许多问题都说这不是在各种条件下重命名文件的好方法。
Android 中还有其他重命名文件的方法吗?如果我只使用这种方式,如何使其发挥作用?

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

        alert = new AlertDialog.Builder(this);
        etRenameFile = new EditText(getApplicationContext());
        alert.setTitle("Do you want to rename the file?");
        alert.setMessage(" ");
        alert.setView(etRenameFile);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog, int whichButton) {
                renameFileAlert();
                adapter.notifyDataSetChanged();
            }
        });

        alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // what ever you want to do with No option.
            }
        });

        private void renameFileAlert(){
            String renameFile = etRenameFile.getText().toString();
            Log.v(TAG, renameFile + " another");
            String filename= al_images.get(int_position).getAl_imagepath().get(fileIndex);

            File oldFilePath = new File(al_images.get(int_position).getAl_imagepath().get(fileIndex));
            Log.v(TAG,oldFilePath.toString());
            // Log.d("OLDFILEPATH", oldFilePath.toString());  // prints the correct path of the file being selected

            x = al_images.get(int_position).getAl_imagepath().get(fileIndex);
            File renamedFile = new File(x.replace(filename, renameFile));
            Log.v(TAG, renamedFile.toString() + " new name");  //prints new name of the file being entered
            // Log.d("NEWFILE", renamedFile.toString());

            boolean renamed = oldFilePath.renameTo(renamedFile);
            if(renamed){
                Log.v(TAG, "rename done");
            } else Log.v(TAG, "failed");

            notifyMediaStoreScanner(renamedFile);
        }

        public final void notifyMediaStoreScanner(File file) {
//        try {
//            MediaStore.Images.Media.insertImage(getBaseContext().getContentResolver(),
//                    file.getAbsolutePath(), file.getName(), null);
        getBaseContext().sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
        getBaseContext().sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
    }

最佳答案

您想将其重命名为什么?当且仅当重命名成功时,renameTo() 才会返回 true;否则返回 false。

关于java - renameTo() 不适用于重命名图像并返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48898085/

相关文章:

java - SOAP:如何在 Java 中调用 CountryInfoService WSDL 文件,例如使用 CapitalCity SOAP 操作

python - 比较从 ftp 服务器获取的数字

c# - 在 C# 中检查文件名是否*可能*有效(不存在)

java - 将 Java 字符串数组或 JSON 字符串转换为 Javascript 数组(Parse.com Cloud Code 和 Mandrill)

java - 升级 Fedora 24,Gradle 状态文本不显示

java - 保存的 BufferedImage 中仅显示最后一个圆

java - 安卓音频电平表

java - 在 UI 线程上获取重复回调的正确方法

java - Android - 如何使用表创建 Sqlite 数据库

c++ - 如何在加载/保存功能中读取文件?