在Android的ImageView和进度执行图像下载 [英] Image download in an Android ImageView and Progressbar implementation

查看:125
本文介绍了在Android的ImageView和进度执行图像下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在的ImageView 来下载图像。我想用一个进度来告诉程序下载图像的用户。如果程序无法在30秒下载图像,程序会使用吐司 / AlertDialog 来通知用户并退出。

I need to download an image in an ImageView. I want to use a ProgressBar to tell the user that the program is downloading images. If the program can't download the image in 30sec, the program will use a Toast/AlertDialog to notify the user and exit.

我怎样才能实现这个功能呢?任何人都可以给我就如何建立框架一些建议吗?我能完成的细节。我需要线程? / AsyncTask的?

How can I implement this function? Could anyone give me some advice on how to build the framework? I can complete the details. Do I need thread? / AsyncTask?

推荐答案

是的,你需要下载的AsyncTask图像(我假设你是从网址下载)。
有效地实现自己的功能,这就是你需要做的:

Yes, you do need to download the image in AsyncTask (I am assuming that you are downloading from URL). Effectively to achieve your functionality this is what you need to do:


  1. 创建的AsyncTask下载图像(实现doInBackground下载()),也有一个布尔值(比如isImageDownloaded)来跟踪如果图像是postExecute成功下载()。不要忘了启动下载之前还显示进度条

  2. 执行您的AsyncTask开始下载

  3. 创建android.os.CountDownTimer延伸至倒数30秒

  4. 在法onFinish()检查您跟踪布尔值,如果是假的,那么你取消AsyncTask的扔,你预期的面包/对话框

下面是伪code /什么我上面提到的步骤骨架(没有检查语法,所以我对任何错误道歉)

Below is the pseudocode/skeleton of what the steps that I mentioned above (didn't check for syntax, so I apologize for any error)



    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()
        }

这篇关于在Android的ImageView和进度执行图像下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆