I am reading the list of file from SD card. But my app freezes while it loops through the storage. So I want to show a progress bar before showing all the file.
public ArrayList<MyFile> getPlayList() {
File home = Environment.getExternalStorageDirectory();
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addFileToList(file);
}
}
}
return songsList;
}
@OnClick(R.id.btn_browser)
public void onClickBrowser() {
// want to show progress bar here when user click a button
if (fileList == null)
fileList = fileManager.getPlayList();
ListDialog listDialog = new ListDialog(this, fileList,
this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
listDialog.show();
}
You need to use asynchronously call getPlayList()
. There are various way to use it and I would suggest AsyncTask
since fetching files should take < 3s. If it's more then you need to think of some other background solution.
In your AsyncTask,
Show the progress indicator during onPreExecute()
,
Perform the getPlayList()
in doInBackground()
,
Finally dismiss the progress indicator and show your ListDialog
in onPostExecute()
.
That being said, if fetching the playlist is a one time thing and you are fairly certain that the user will anyway click on R.id.btn_browser
, then you could prefetch the playlist. That's just a design suggestion.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.