我需要下载ImageView
中的图像。我想使用ProgressBar
来告诉用户程序正在下载图像。如果程序无法在30秒内下载图像,程序将使用Toast
/AlertDialog
来通知用户并退出。在Android ImageView和Progressbar实现中下载图像
我该如何实现这个功能?任何人都可以给我一些关于如何构建框架的建议吗?我可以完成细节。我需要线程吗?/AsyncTask?
我需要下载ImageView
中的图像。我想使用ProgressBar
来告诉用户程序正在下载图像。如果程序无法在30秒内下载图像,程序将使用Toast
/AlertDialog
来通知用户并退出。在Android ImageView和Progressbar实现中下载图像
我该如何实现这个功能?任何人都可以给我一些关于如何构建框架的建议吗?我可以完成细节。我需要线程吗?/AsyncTask?
我希望你试图从一个已知的网址下载图片,我是对吗?如果是这样,请有look at this url
希望帮助你...
您还可以看到this。它将涵盖将图像下载到手机的过程,以及在下载图像时提供加载线程的过程。
是的,你需要下载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()
}