Reputation: 4082
I am working on a task to read SD card files and I am showing a progress bar with calculated percentage based on SD card files like some antivirus software in Android. I don't know how to implement this functionality. My SD card read files code shown below:
File root = new File (Environment.getExternalStorageDirectory().getAbsolutePath());
scan(root);
Scan function code shown below:
public void scan (File path) {
for (File f : path.listFiles()) {
if (f.isFile()) {
Toast.makeText(getApplicationContext(),f.getAbsoluteFile().toString(),Toast.LENGTH_SHORT).show();
}
else {
}
}
}
Can you show me how to do this?
Upvotes: 1
Views: 1489
Reputation: 11359
public void scan (final File path) {
float progress = 0.0f;
int count = 0;
for (File f : path.listFiles()) {
if (f.isFile()) {
Toast.makeText(getFragmentContext(),f.getAbsoluteFile().toString(),Toast.LENGTH_SHORT).show();
}
else {
}
count++;
progress += ( (float)count / (float)path.listFiles().length ) * 100;
}
}
Hope This is what you want.
Upvotes: 1
Reputation: 1547
I guess that a file list has a length. You can use this length in your loop to compute a percentage of number of files completed so far. Perhaps something like:
File[] files = path.listFiles();
for (i=0; i<files.length; i++) {
// Report i / files.length percent done
}
Upvotes: 2