Image download in an Android ImageView and Progressbar implementation

此生再无相见时 提交于 2019-12-08 14:35:30

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. Create AsyncTask to download your image (implement download in doInBackground()), also have a boolean (say isImageDownloaded) to track if the image is successfully downloaded in postExecute(). Don't forget to also show your progress bar before initiating the download
  2. Execute your AsyncTask to initiate download
  3. Create extension of android.os.CountDownTimer to countdown 30 secs
  4. On the method onFinish() check the boolean that you track, if it is false then you cancel the AsyncTask and throw the toast/dialog that you intended

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

Maybe this helps a little bit.

The code for the progress bar you can find here.

Nick

You could also see this. It will cover the process of downloading the image to the phone as well as providing loading threads while the image is being downloaded.

Dinash

I hope you are trying to download an image from a known url, am i right? If so please have a look at this url

Hope that helps you...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!