如何使用java中的ProgressBar将文件从一个路径复制到另一个路径
我希望你很好。 我这里有一个我从这个网站上获取的代码,用于将文件从一个路径复制到另一个路径。 我想将progressBar与之配合使用,如何将progressBar与计数器配合使用 我使用了下面的代码,progressBar中没有任何进展! 代码如下:如何使用java中的ProgressBar将文件从一个路径复制到另一个路径,java,android,Java,Android,我希望你很好。 我这里有一个我从这个网站上获取的代码,用于将文件从一个路径复制到另一个路径。 我想将progressBar与之配合使用,如何将progressBar与计数器配合使用 我使用了下面的代码,progressBar中没有任何进展! 代码如下: progressbar1 = (ProgressBar) findViewById(R.id.progressbar1); progressbar1.setMax((int)100); java.io.File filein
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");
}
在Android中,对于长时间运行的运营商,如从网络下载文件、访问数据库、复制文件,你应该使用后台线程来防止应用程序冻结甚至崩溃 要更新进度条的进度,您应该在UI/主线程中通过使用服务器机制(如从活动类或从处理程序类)来完成 因此,您的代码将是:
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();
}
}
在Android中,对于长时间运行的运营商,如从网络下载文件、访问数据库、复制文件,你应该使用后台线程来防止应用程序冻结甚至崩溃 要更新进度条的进度,您应该在UI/主线程中通过使用服务器机制(如从活动类或从处理程序类)来完成 因此,您的代码将是:
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();
}
}
progressbar1.setProgress((int)k)代码>你已经用计数器的值设置了进度条。那么到底是什么问题呢?进度条没有启动!(没有进展)嘿!你能再发一次网站链接吗。我想你没有把它连接好。但通常情况下,通过多线程将计算从渲染中分离出来。在哪个站点上?我刚从这里得到了没有progressBar的代码“stackoverflow”,然后我尝试将它与progressBar链接,但没有成功我不知道错误在哪里进度条没有启动!(没有进展)
我想它会立即显示最大值。如果将“最大值”设置为100,则在复制100字节后将达到最大值。我认为这些文件比100多一点。progressbar1.setProgress((int)k)代码>你已经用计数器的值设置了进度条。那么到底是什么问题呢?进度条没有启动!(没有进展)嘿!你能再发一次网站链接吗。我想你没有把它连接好。但通常情况下,通过多线程将计算从渲染中分离出来。在哪个站点上?我刚从这里得到了没有progressBar的代码“stackoverflow”,然后我尝试将它与progressBar链接,但没有成功我不知道错误在哪里进度条没有启动!(没有进展)
我想它会立即显示最大值。如果将“最大值”设置为100,则在复制100字节后将达到最大值。我觉得文件比100多了一点。非常感谢兄弟,工作得很好,只是progressBar在上半场停下来,然后重新运行,但没问题我很尊重你谢谢兄弟,工作得很好,只是progressBar在上半场停下来,然后重新运行,但没问题,我很尊重你