gpt4 book ai didi

java - 在 Android 上移动文件时询问用户是否重写文件

转载 作者:行者123 更新时间:2023-12-02 04:48:09 25 4
gpt4 key购买 nike

我正在移动文件1) 将初始文件复制到文件夹2)最后删除初始文件。如果目标文件夹中已存在同名文件,我将创建一个警报对话框“重写此文件?”。但当出现消息时,初始文件正在被删除。

我开始在同一个新线程(...)中复制和删除方法,并在 UIthread 中显示警报对话框。

//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());
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);
}
}

关于java - 在 Android 上移动文件时询问用户是否重写文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56465801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com