在Android上移动文件时询问用户有关重写文件的信息

时间:2019-06-05 18:03:54

标签: java android file

我正在移动文件 1)将初始文件复制到文件夹 2)最后删除初始文件。 如果目标文件夹中已经存在相同名称的文件,则创建一个Alertdialog“是否重写此文件?”。但是当出现消息时,初始文件将被删除。

我在同一新Thread(...)中开始复制和删除方法,并在UIthread中显示alertdialog。

//First class:
final File file = new File(initFilePath);
new Thread(new Runnable() {
@Override
public void run() {
    FileFoldersLab.get(getContext()).copyFile(file,FileFoldersLab.get(getContext()).getCurPath());
    FileFoldersLab.get(getContext()).removeFile(file);
    updateUI();
}
}).start();

//FileFoldersLab:
public void copyFile(final File src, final String destPath){
final File f = new File(destPath+File.separator+src.getName());
if(f.exists()){
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("The file already exists");
        builder.setMessage("Would you like to rewrite " + f.getPath()+ " ?");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Copying file
            }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
    //Showing the alert dialog
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    }else{
        //Copying file
    }
} 

1 个答案:

答案 0 :(得分:0)

您应该将删除文件,重写确认和未找到同一文件的条件放入

更改此

   //First class:
final File file = new File(initFilePath);
new Thread(new Runnable() {
@Override
public void run() {
    FileFoldersLab.get(getContext()).copyFile(file,FileFoldersLab.get(getContext()).getCurPath());
    FileFoldersLab.get(getContext()).removeFile(file);
    updateUI();
}
}).start();

//FileFoldersLab:
public void copyFile(final File src, final String destPath){
final File f = new File(destPath+File.separator+src.getName());
if(f.exists()){
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("The file already exists");
        builder.setMessage("Would you like to rewrite " + f.getPath()+ " ?");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Copying file
            }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
    //Showing the alert dialog
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    }else{
        //Copying file
    }
} 

进入此

        //First class:
        final File file = new File(initFilePath);
        new Thread(new Runnable() {
        @Override
        public void run() {
            FileFoldersLab.get(getContext()).copyFile(file,FileFoldersLab.get(getContext()).getCurPath());

            updateUI();
        }
        }).start();

        //FileFoldersLab:
        public void copyFile(final File src, final String destPath){
        final File f = new File(destPath+File.separator+src.getName());
        if(f.exists()){
                final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("The file already exists");
                builder.setMessage("Would you like to rewrite " + f.getPath()+ " ?");
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Copying file

     //After copying 
FileFoldersLab.get(getContext()).removeFile(file);
                    }
                });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
            //Showing the alert dialog
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    }
                });
            }else{
                //Copying file
    //After copying
    FileFoldersLab.get(getContext()).removeFile(file);
            }
        }