Download images with AsyncTask

11,052

Solution 1

You never called onProgressUpdate during your doInBackGround(...). Please note that running multiple instances of AsyncTask is a bad idea. Here is what I suggest:

if(isSyncSuccess){
    SetConstant.IMAGE_EXIST=1;
    pDialog=new ProgressDialog(GalleryScreen.this);
    pDialog.setMessage("Downloading file. Please wait...");
    pDialog.setIndeterminate(false);
    pDialog.setProgress(0);
    pDialog.setMax(contentId.length);
    pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pDialog.setCancelable(true);

    new DownloadFile().execute();
}

private class DownloadFiles extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... sUrl) {
        Bitmap bm;
        InputStream in;

        if (contentId.length > 0) {
            for (int i = 0; i < contentId.length; i++) {
                if (helper.databaseChecking(useremail, contentId[i])) {
                    contentdownload = i;
                    SetConstant.CONTENT_ID = contentId[i];

                    String URL = SetConstant.URL_DOWNLOAD_CONTENT + contentId[i];
                    //YOUR INTRESTING LOOP HERE.
                    publishProgress(30);
                    //SOME INTRESTING NUMBER FOR PROGRESS UPDATE
                }
            }

            try {
                in = new java.net.URL(sUrl[0]).openStream();
                bm = BitmapFactory.decodeStream(new PatchInputStream(in));
                File storage = new File(Environment.getExternalStorageDirectory() + File.separator + "/Image/");
                Log.i(TAG, "storage:" + storage);
                Log.i(TAG, "storage:" + storage.getAbsolutePath());
                if (!storage.exists()) {
                    storage.mkdirs();

                }
                String FileName = "/" + SetConstant.CONTENT_ID + ".jpg";
                FileOutputStream fos = new FileOutputStream(storage + FileName);
                bm.compress(Bitmap.CompressFormat.JPEG, 85, fos);

                String filepath = storage + FileName;
                File filecheck = new File(filepath);
                long fileSize = filecheck.length();
                fos.flush();
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPreExecute () {
            super.onPreExecute();
            pDialog.show();
        }

        @Override
        protected void onProgressUpdate (Integer...progress){
            super.onProgressUpdate(progress);
            pDialog.setProgress(progress[0]);
        }

        protected void onPostExecute (String result){
            super.onPostExecute(result);
            pDialog.dismiss();
        }
    }
}

Of course this code don't run and you need to fix the scopes. But what I am trying to suggest is that your loop should be in doInBackGround(...), you should only have 1 instance of AsyncTask at given time for this case, and call the onProgressUpdate().

Solution 2

Primary issue :

SetConstant.CONTENT_ID = contentId[i]; 
String URL = SetConstant.URL_DOWNLOAD_CONTENT+contentId[i];

Here, you are facing trouble. As @Sofi Software LLC's answer, you are using a global variable, whose value is being changed by the main thread, in another thread.

Secondary Issue :

  • If you want a progress bar to update, you have to update its value;
    it doesn't update itself.

You do need to download the image in AsyncTask (Downloading from URL). Effectively to achieve your functionality, you need to do

  • 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
  • Execute your AsyncTask to initiate download
  • Create extension of android.os.CountDownTimer to countdown a minimum time
  • 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
  • Running multipule instance of AsyncTask is not a good idea, so do one after another. You can execute your AsyncTask's on an Executor using executeOnExecutor().To make sure that the threads are running in a serial fashion please use: SERIAL_EXECUTOR.

Following resources may help you #

If you need to download an image, show progress bar and load in a imageview

If you need to download multiple files (here, for images) using AsyncTask

EDIT:

From http://developer.aiwgame.com/imageview-show-image-from-url-on-android-4-0.html

new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
            .execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"); }

public void onClick(View v) {
    startActivity(new Intent(this, IndexActivity.class));
    finish();

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    } }

From Image download in an Android ImageView and Progressbar implementation

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

Solution 3

In the following line:

SetConstant.CONTENT_ID = contentId[i];

You're setting a global variable to a value, then you create a string url based on that same value and pass it to the AsyncTask. That executes, and then when it is done downloading, it create a file whose name is based on the global variable SetConstant.CONTENT_ID.

In other words, you are using a global variable, whose value is being changed by the main thread, in another thread. Don't do this, as you will get all kinds of weird problems due to the different threads updating at different times.. Pass in the value or the name of the output file to the AsyncTask. You can do that in a constructor for DownloadFile, and stash the value in a field.

If you want a progress bar to update, you have to update its value; it doesn't update itself. Call AsyncTask.publishProgress during the task (in doInBackground) and implement onProgressUpdate to update the progress dialog.

[EDIT: onProgressUpdate is indeed called in the UI thread.]

Share:
11,052
IssacZH.
Author by

IssacZH.

Updated on June 30, 2022

Comments

  • IssacZH.
    IssacZH. almost 2 years

    I'm not really sure what goes wrong with my code or structure. I wanted to use AsyncTask to download images and display out the progress bar at the mean time. But I tried out a few different way of doing it. It still failed and no idea what's wrong with it. My structure flow is

    ContentID is a string array that stores the content ID of the Images.

    Primary Issue: It managed to download images from the url and store into the phone, but the downloaded images are all the same image. It should be different images, it's not what I expected.

    Secondary Issue: The progress bar pop up while the application downloading images, but the progress bar did not update it's progress. It just remains 0% and dismissed after the download completed.

    I wanted to know what causes primary and secodary issue as i mentioned. Please leave a comment or answer if you might know what's wrong with my code. Any help will be appreciated.

    if(isSyncSuccess){
    
         SetConstant.IMAGE_EXIST = 1;
         pDialog = new ProgressDialog(GalleryScreen.this);
         pDialog.setMessage("Downloading file. Please wait...");
         pDialog.setIndeterminate(false);
         pDialog.setProgress(0);
         pDialog.setMax(contentId.length);
         pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
         pDialog.setCancelable(true);
    
    
         if (contentId.length>0){
         Log.i(TAG, "contentid.length:" +contentId.length);
             for (int i=0;i<contentId.length;i++){
                 if(helper.databaseChecking(useremail, contentId[i])){
                     contentdownload = i;
                     SetConstant.CONTENT_ID = contentId[i]; 
    
                     String URL = SetConstant.URL_DOWNLOAD_CONTENT+contentId[i];
    
                     DownloadFile downloadFile = new DownloadFile();
                     downloadFile.execute(URL);
    
    
                     }
    
    
    
        private class DownloadFile extends AsyncTask<String, Integer, String>{
        @Override
        protected String doInBackground(String... sUrl){
                    Bitmap bm;
                    InputStream in;
    
            try{
    
                in = new java.net.URL(sUrl[0]).openStream();
                bm = BitmapFactory.decodeStream(new PatchInputStream(in));
                File storage = new File(Environment.getExternalStorageDirectory() + File.separator + "/Image/");
                Log.i(TAG,"storage:" +storage);
                Log.i(TAG,"storage:" +storage.getAbsolutePath());
                if(!storage.exists()){
                    storage.mkdirs();
    
                }
                    String FileName = "/"+SetConstant.CONTENT_ID+".jpg"; 
                    FileOutputStream fos = new FileOutputStream(storage + FileName);
                    bm.compress(Bitmap.CompressFormat.JPEG, 85, fos);
    
                    String filepath = storage + FileName;
                    File filecheck = new File (filepath);
                    long fileSize = filecheck.length();
                    fos.flush();
                    fos.close();
    
                    Log.i(TAG, "bm:" +bm);
                    Log.i(TAG, "fos:" +fos);
                    Log.i(TAG, "filesize:" +fileSize);
                    Log.i(TAG, "filepath:" +filepath);
    
    
            }
            catch(IOException e1){
                    e1.printStackTrace();
                    }   
    
            return null;
        }
    
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            pDialog.show();
        }
    
        @Override
        protected void onProgressUpdate(Integer... progress){
            super.onProgressUpdate(progress);
            pDialog.setProgress(progress[0]);
        }
    
        protected void onPostExecute(String result){
            super.onPostExecute(result);
            pDialog.dismiss();
        }
    }
    

    Edit

    Now the application able to download images according and the progress bar is working as well! But I got another issue is how to return error message when the application failed to complete the download. Currently when the application failed to download it will crash. I believed that I should not run it inside the doInBackground side. But where else can I do the checking? Any idea how to return as an error message and request for the user to retry instead of crashing the application?