Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:56:59+00:00 2026-06-01T20:56:59+00:00

Async Image Download and storing in an array of ImageView I have a problem

  • 0

Async Image Download and storing in an array of ImageView

I have a problem loading bitmap images from an url. The point is that the code works, because I call 2 requests from the server to retrieve some information, during the first request, everything works fine, but during the second one, it seems to be that connection.connect();
doesn’t do anything…

Here is the code:

FIRST RETRIEVE

private class RequestQuickEventService extends AsyncTask<Void, Void, Boolean> {

    protected Boolean doInBackground(Void... gameId) {
        quickEvent = MyRestClient.getInstance().retrieveQuickEvent();
        ...
        if (quickEvent == null) {
            return false;
        }
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if (result) {
            prepareQuestion();
        }
    }   

}

private void prepareQuestion() {
    new DownloadImageService().execute(quickEvent.getImage());
}

private class DownloadImageService extends AsyncTask<String, Void, Bitmap>  {

    private Bitmap loadImageFromNetwork(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } 
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
    protected Bitmap doInBackground(String... urls) {
        return loadImageFromNetwork(urls[0]);
    }

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

}

SECOND RETRIEVE

private class SendQuickQuestionService extends AsyncTask<Integer, Void, Void> {

    protected void onPreExecute() {
        ...
        taskResponse = new TaskResponse(ti, te, 1);
        ...
    }

    protected Void doInBackground(Integer... params) {
        ...
        quickEvent = MyRestClient.getInstance().sendQuickEvent(...);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (quickEvent != null) {
            prepareQuestion(); //same as before
        }
    }

}

I don’t know why, but the second time I call loadImageFromNetwork, it returns null image:

- First url: https://.../e4774cdda0793f86414e8b9140bb6db42012-02-20_161209.586668.jpeg-->OK
- Second url: https://.../e4774cdda0793f86414e8b9140bb6db42012-02-20_143228.782917.png-->FAIL
connection.connect();   //THE SECOND TIME DOES NOTHING...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:57:01+00:00Added an answer on June 1, 2026 at 8:57 pm

    Here is the solution:

    enter/** Classes **/
    private class GetProfilePicAsyncTask extends AsyncTask<Void, Void, Bitmap> {
    
        @Override
        protected Bitmap doInBackground(Void... params) {
            return Utility.getBitmap(User.getInstance().getAvatar());
        }
    
        @Override
        protected void onPostExecute(Bitmap result) {
            if (result != null) {
                avatar.setImageBitmap(result);
            }
            else {
                ...
            }
        }
    
    }
    

    and

    in whatever class (Utils for example…):

    /** Variables **/
     public static AndroidHttpClient httpclient = null;
    
    
    /** Public Functions **/
    public static Bitmap getBitmap(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
            bis.close();
            is.close();
        } 
        catch (Exception e) {
            e.printStackTrace();
        } 
        finally {
            if (httpclient != null) {
                httpclient.close();
            }
        }
        return bm;
    }
    
    /** Classes **/
    private static class FlushedInputStream extends FilterInputStream {
        public FlushedInputStream(InputStream inputStream) {
            super(inputStream);
        }
    
        @Override
        public long skip(long n) throws IOException {
            long totalBytesSkipped = 0L;
            while (totalBytesSkipped < n) {
                long bytesSkipped = in.skip(n - totalBytesSkipped);
                if (bytesSkipped == 0L) {
                    int b = read();
                    if (b < 0) {
                        break; // we reached EOF
                    } else {
                        bytesSkipped = 1; // we read one byte
                    }
                }
                totalBytesSkipped += bytesSkipped;
            }
            return totalBytesSkipped;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView that is displaying images downloaded from the internet. My UI
I have UITableViewCell s that have an imageView that loads a UIImage from a
I'm trying to create custom view that draws image downloaded from Url. The code
i m parsing a json feed .using async image download I m downloading images
so I have a function that exports some data async via asp.net and displays
I have a UITableView that has photos, i get these photos from URLs and
I am trying to implement a ListView with async image loading inside a Fragment.
Is it possible to use async image loader from this project http://open-pim.com/tmp/LazyList.zip with Gallery
I'm using Three20's TTImageView for it's async image loading + caching. I've noticed this
I have an async call that, when it completes, I want to set a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.