【问题标题】:how to Copy files from path to path with ProgressBar in java如何在java中使用ProgressBar将文件从路径复制到路径
【发布时间】:2020-11-09 19:18:03
【问题描述】:

我希望你没事。 我在这里有一个代码,我从这个站点获取它,用于从路径复制文件。 我想和它一起使用progressBar,我如何将progressBar 与计数器一起使用?

我使用了下面的代码,progressBar 没有任何进展! 这是代码:

progressbar1 = (ProgressBar) findViewById(R.id.progressbar1);
    progressbar1.setMax((int)100);  

    java.io.File filein  = new java.io.File("/storage/emulated/0/Alarms/test.zip");
    java.io.File fileout = new java.io.File("/storage/emulated/0/testcopied.zip");
    java.io.FileInputStream  fin  = null;
    java.io.FileOutputStream fout = null;
    long length  = filein.length();
    long counter = 0;
    int r = 0;
    byte[] b = new byte[1024];
    try {
            fin  = new java.io.FileInputStream(filein);
            fout = new java.io.FileOutputStream(fileout);
            while( (r = fin.read(b)) != -1) {
                    counter += r;

                    int k = (int)counter;
                    progressbar1.setProgress((int)k);
                    System.out.println( 1.0 * counter / length );
                    fout.write(b, 0, r);

            }
    }
    catch(Exception e){
            System.out.println("foo");
    }

【问题讨论】:

  • progressbar1.setProgress((int)k); 嗯,你已经用计数器的值设置了进度条。那么到底是什么问题呢?
  • 进度条不启动! (没有进展)
  • 嘿!能不能再发一下网站链接。我认为你没有正确链接它。但通常你会通过多线程将计算与渲染分开。
  • @Valentin 哪个网站兄弟?我刚刚从这里“stackoverflow”得到了没有progressBar的代码,然后我尝试将它与progressBar链接但没有成功我不知道错误在哪里
  • the progress Bar don't Start ! (there's no progress) 我认为它会立即指示最大值。如果将最大值设置为 100,则复制 100 个字节后将达到最大值。我认为文件超过 100 个。

标签: java android


【解决方案1】:

在Android中,对于长时间运行的操作,例如从网络下载文件、访问数据库、复制文件,您应该使用后台线程来防止应用程序冻结甚至崩溃。

要更新进度条的进度,您应该在 UI/主线程中使用一些机制来完成,例如来自 Activity 类的runOnUiThread(Runnable) 或来自 Handler 类的post(Runnable)

所以你的代码将是:

public class MainActivity extends AppCompatActivity {

    ProgressBar progressbar1;

    // Use a background thread to copy files
    Thread copyingThread;

    // Use Handler to update progress bar's progress
    Handler mainHandler = new Handler(Looper.getMainLooper());

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

        progressbar1 = (ProgressBar) findViewById(R.id.progressbar1);
        progressbar1.setMax(100);
        progressbar1.setProgress(0);

        copyingThread = new Thread(new Runnable() {
            @Override
            public void run() {
                File sourceFile = new java.io.File("/storage/emulated/0/Alarms/test.zip");
                File destFile = new java.io.File("/storage/emulated/0/testcopied.zip");
                FileInputStream fileInputStream;
                FileOutputStream fileOutputStream;
                long length = sourceFile.length();
                int bytesRead;
                int totalBytesRead = 0;
                byte[] buffer = new byte[4 * 1024]; // 4KB buffer
                try {
                    fileInputStream = new FileInputStream(sourceFile);
                    fileOutputStream = new FileOutputStream(destFile);

                    while (!Thread.currentThread().isInterrupted()
                            && (bytesRead = fileInputStream.read(buffer)) != -1) {
                        // Write bytesRead to destination file
                        fileOutputStream.write(buffer, 0, bytesRead);

                        // Calculate the copying percent
                        totalBytesRead += bytesRead;
                        int percent = (int) (totalBytesRead * 100 / length);
                        Log.i("DEBUG", "Copied: " + percent + "%");

                        // Update progress bar's progress in UI/main thread
                        mainHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                progressbar1.setProgress(percent);
                            }
                        });
                    }
                } catch (Exception e) {
                    System.out.println("foo");
                }
            }
        });
        copyingThread.start();
    }

    @Override
    protected void onDestroy() {
        // Release copying thread's resource in case users leaving this activity
        if (copyingThread != null && copyingThread.isAlive()) {
            copyingThread.interrupt();
        }
        super.onDestroy();
    }
}

【讨论】:

  • 非常感谢兄弟,工作得很好,只是进度条在一半停止并重新运行,但没问题,我尊重你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 2013-05-29
  • 1970-01-01
相关资源
最近更新 更多