Java 复制文件后显示成功消息

Java 复制文件后显示成功消息,java,android,toast,Java,Android,Toast,我对安卓系统的开发相当陌生,所以我想我应该从一个基本的应用程序开始。当按下按钮时,它会将文件复制到代码中写入的位置(请参见下面的“我的代码”)。当我按下安装按钮并将文件复制到其位置时,我希望toast消息显示“成功安装或复制文件时出错”。我将如何实现这一点 public class TrialActivity extends Activity { private ProgressDialog progressDialog; /** Called when the activity is fir

我对安卓系统的开发相当陌生,所以我想我应该从一个基本的应用程序开始。当按下按钮时,它会将文件复制到代码中写入的位置(请参见下面的“我的代码”)。当我按下安装按钮并将文件复制到其位置时,我希望toast消息显示“成功安装或复制文件时出错”。我将如何实现这一点

public class TrialActivity extends Activity {

private ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    runDialog(5);
}

private void runDialog(final int seconds)
{
    progressDialog = ProgressDialog.show(this, "Please Wait...", "unpacking patch in progress");

    new Thread(new Runnable(){
        public void run(){
            try {
                Thread.sleep(seconds * 1000);
                progressDialog.dismiss();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

    ((Button)findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramView)
      {

      }

   {

              InputStream in = null;
              OutputStream out = null;
  String filename="savegame.bin";           
  try {    

                in = getResources().openRawResource(R.raw.savegame);
                out = new FileOutputStream("/sdcard/Android/data/files/" + filename);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
              } catch(Exception e) {
                  Log.e("tag", e.getMessage());
              }       

      }
      private void copyFile(InputStream in, OutputStream out) throws IOException {
          byte[] buffer = new byte[1024];
          int read;
          while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);


          }
      }
      }
    );
  }

}
使用下一个代码:

    private class AsyncTaskDialog extends AsyncTask<Void, Void, Void> {
    Toast toastSuccess;


    boolean flagSuccessCopy =false;
            protected void onPreExecute() {
                super.onPreExecute();


                }
            }

            protected Boolean doInBackground(Void... params) {
                copyFiles();
                return null;
            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result); 

              if (getActivity() != null) {
                 if(flagSuccessCopy){
                  toastSuccess  = Toast.makeText(context, "Success", duration);
                 }else{
                      toastSuccess  = Toast.makeText(context, "Error", duration);
                 }
                toast.show();               
            }
        }
私有类AsyncTaskDialog扩展了AsyncTask{
为成功干杯;
布尔flagSuccessCopy=false;
受保护的void onPreExecute(){
super.onPreExecute();
}
}
受保护的布尔doInBackground(Void…params){
复制文件();
返回null;
}
@凌驾
受保护的void onPostExecute(布尔结果){
super.onPostExecute(结果);
如果(getActivity()!=null){
如果(flagSuccessCopy){
Toast Success=Toast.makeText(上下文,“Success”,持续时间);
}否则{
toastSuccess=Toast.makeText(上下文,“错误”,持续时间);
}
toast.show();
}
}

允许将任何文件从任何应用程序复制到SD卡

import ru.gelin.android.sendtosd.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ProgressDialog extends AlertDialog implements Progress {

/** Activity for which the dialog is created */

Activity activity;

/** Progress manager for the dialog */
ProgressManager manager = new ProgressManager();

/**
 *  Creates the customized progress dialog for
 *  activity.
 */
protected ProgressDialog(Activity activity) {
    super(activity);
}

@Override
public void setFiles(int files) {
    synchronized (manager) {
        manager.setFiles(files);
    }
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            synchronized (manager) {
                updateTotalProgress();
            }
        }
    });
}

@Override
public void nextFile(final File file) {
    synchronized (manager) {
        manager.nextFile(file);
    }
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            synchronized (manager) {
                updateFileName(file);
                updateFileProgress();
                updateTotalProgress();
            }
        }
    });
}

@Override
public void processBytes(long bytes) {
    synchronized (manager) {
        manager.processBytes(bytes);
    }
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            synchronized (manager) {
                updateFileProgress();
            }
        }
    });
}

void updateTotalProgress() {
    ProgressBar progress = (ProgressBar)findViewById(R.id.total_progress);
    progress.setMax(manager.getFiles());
    progress.setProgress(manager.getFile());
    TextView text = (TextView)findViewById(R.id.total_files);
    text.setText(getContext().getString(R.string.files_progress,
            manager.getFile(), manager.getFiles()));
}

void updateFileName(File file) {
    if (file == null) {
        return;
    }
    TextView view = (TextView)findViewById(R.id.file_name);
    view.setText(file.getName());
}

void updateFileProgress() {
    ProgressBar progress = (ProgressBar)findViewById(R.id.file_progress);
    if (manager.getProgressInUnits() < 0) {
        progress.setIndeterminate(true);
    } else {
        progress.setIndeterminate(false);
        progress.setMax((int)(manager.getSizeInUnits() * 10));
        progress.setProgress((int)(manager.getProgressInUnits() * 10));
    }
    TextView text = (TextView)findViewById(R.id.file_size);
    text.setText(getContext().getString(manager.getSizeUnit().progressString,
            manager.getProgressInUnits(), manager.getSizeInUnits()));
    }

}
导入ru.gelin.android.sendtosd.R;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.widget.ProgressBar;
导入android.widget.TextView;
公共类ProgressDialog扩展AlertDialog实现进度{
/**为其创建对话框的活动*/
活动;
/**对话框的进度管理器*/
ProgressManager=新的ProgressManager();
/**
*为创建自定义进度对话框
*活动。
*/
受保护的进程对话框(活动){
超级(活动);
}
@凌驾
公共void集合文件(int文件){
已同步(管理器){
manager.setFiles(文件);
}
activity.runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
已同步(管理器){
updateTotalProgress();
}
}
});
}
@凌驾
public void nextFile(最终文件){
已同步(管理器){
manager.nextFile(文件);
}
activity.runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
已同步(管理器){
updateFileName(文件);
updateFileProgress();
updateTotalProgress();
}
}
});
}
@凌驾
公共void processBytes(长字节){
已同步(管理器){
manager.processBytes(字节);
}
activity.runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
已同步(管理器){
updateFileProgress();
}
}
});
}
void updateTotalProgress(){
ProgressBar进度=(ProgressBar)findViewById(R.id.总进度);
progress.setMax(manager.getFiles());
progress.setProgress(manager.getFile());
TextView text=(TextView)findViewById(R.id.total_文件);
text.setText(getContext().getString(R.string.files\u progress,
manager.getFile(),manager.getFiles());
}
void updateFileName(文件){
if(file==null){
返回;
}
TextView视图=(TextView)findViewById(R.id.file\u name);
view.setText(file.getName());
}
void updateFileProgress(){
ProgressBar进度=(ProgressBar)findViewById(R.id.file\u进度);
if(manager.getProgressInUnits()<0){
progress.setUndeterminate(true);
}否则{
进度。设置不确定(false);
progress.setMax((int)(manager.getSizeInUnits()*10));
progress.setProgress((int)(manager.getProgressInUnits()*10));
}
TextView text=(TextView)findViewById(R.id.file_size);
text.setText(getContext().getString)(manager.getSizeUnit().progressString,
manager.getProgressInUnits(),manager.getSizeInUnits());
}
}

来源:

查看此链接…你完全失去了对大括号的控制
{}
。。。您有两个复制方法,它们都在
OnClickListener
中,但不是
onClick()
方法的一部分。。。无论如何,处理完文件后,只需比较原始文件和复制文件的大小。请提供代码说明以及问题作者应使用它的原因。作者希望复制文件,并获得有关复制文件成功与否的消息,因为最好在后台使用AsyncTask进行复制。我在现场作者添加了进度对话框及时为用户复制文件。或为复制文件创建服务,必要时给他打电话。可能是Comand,如有必要,将多个文件复制到不同的位置,而不是一次复制所有文件。只是我的观点听起来不错。今后,请尝试在您的答案中添加简短的描述。