我需要在ImageView
下载图片。我想使用ProgressBar
告诉用户该程序正在下载图像。如果程序无法在30秒内下载图像,程序将使用Toast
/ AlertDialog
通知用户并退出。
如何实现此功能?谁能给我一些关于如何构建框架的建议?我可以完成细节。我需要线程吗? / AsyncTask?
答案 0 :(得分:1)
是的,您确实需要在AsyncTask中下载图像(我假设您是从URL下载的)。 有效地实现您的功能,这是您需要做的:
下面是我上面提到的步骤的伪代码/骨架(没有检查语法,所以我为任何错误道歉)
public void downloadAndCheck() {
AsyncTask downloadImageAsyncTask =
new AsyncTask() {
@Override
protected Boolean doInBackground(Void... params) {
// download image here, indicate success in the return boolean
}
@Override
protected void onPostExecute(Boolean isConnected) {
// set the boolean result in a variable
// remove the progress bar
}
};
try {
downloadImageAsyncTask.execute();
} catch(RejectedExecutionException e) {
// might happen, in this case, you need to also throw the alert
// because the download might fail
}
// note that you could also use other timer related class in Android aside from this CountDownTimer, I prefer this class because I could do something on every interval basis
// tick every 10 secs (or what you think is necessary)
CountDownTimer timer = new CountDownTimer(30000, 10000) {
@Override
public void onFinish() {
// check the boolean, if it is false, throw toast/dialog
}
@Override
public void onTick(long millisUntilFinished) {
// you could alternatively update anything you want every tick of the interval that you specified
}
};
timer.start()
}
答案 1 :(得分:0)
我希望您尝试从已知网址下载图片,对不对?如果有,请look at this url
希望能帮到你......
答案 2 :(得分:0)
答案 3 :(得分:0)
您还可以看到this。它将涵盖将图像下载到手机以及在下载图像时提供加载线程的过程。