i hope you are fine. i have here a code i took it from this site, for copy files from path to path. i want to use a progressBar with it, how do i use the progressBar with the counter?
i used the bellow code, and there’s is no progress in progressBar ! this is the code :
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");
}
Advertisement
Answer
In Android, for long-running operators such as download files from the network, access database, copying file, you should use a background thread to prevent the app from freeze or even crash.
To update progress bar’s progress, you should do it in UI/main thread by using serveral mechanism such as runOnUiThread(Runnable) from Activity class or post(Runnable) from Handler class.
So your code will be:
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();
}
}