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 845327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:25:55+00:00 2026-05-15T06:25:55+00:00

I am working on an application that downloads images from a url. The problem

  • 0

I am working on an application that downloads images from a url. The problem is that only some images are being correctly downloaded and others are not.
First off, here is the problem code:

public Bitmap downloadImage(String url) {
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = null;
     try {
         response = client.execute(new HttpGet(url));
     } catch (ClientProtocolException cpe) {
        Log.i(LOG_FILE, "client protocol exception");
         return null;
     } catch (IOException ioe) {
            Log.i(LOG_FILE, "IOE downloading image");
            return null;
     } catch (Exception e) {
            Log.i(LOG_FILE, "Other exception downloading image");
            return null;
     }

     // Convert images from stream to bitmap object
     try {
         Bitmap image = BitmapFactory.decodeStream(response.getEntity().getContent());
         if(image==null)
             Log.i(LOG_FILE, "image conversion failed");
         return image;
     } catch (Exception e) {
         Log.i(LOG_FILE, "Other exception while converting image");
         return null;
     }
}

So what I have is a method that takes the url as a string argument and then downloads the image, converts the HttpResponse stream to a bitmap by means of the BitmapFactory.decodeStream method, and returns it. The problem is that when I am on a slow network connection (almost always 3G rather than Wi-Fi) some images are converted to null–not all of them, only some of them. Using a Wi-Fi connection works perfectly; all the images are downloaded and converted properly.

Does anyone know why this is happening? Or better, how can I fix this? How would I even go about testing to determine the problem? Any help is awesome; thank you!

  • 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-05-15T06:25:55+00:00Added an answer on May 15, 2026 at 6:25 am

    This is a known issue with the JPEG decoder. There are two solutions. Either you download the entire image in a byte[] array using a ByteInputStream and then decode the array (this is what I do in code.google.com/p/shelves.) Another solution is to create a wrapper InputStream as shown below:

    public class PatchInputStream extends FilterInputStream {
    
      public PatchInputStream(InputStream in) {
        super(in);
      }
    
      public long skip(long n) throws IOException {
        long m = 0L;
        while (m < n) {
          long _m = in.skip(n-m);
          if (_m == 0L) break;
          m += _m;
        }
        return m;
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an application that downloads some data from the internet using NSData's
I'm working on a web application that has a lot to download (javascript, images,
My application is a Silverlight Image viewer that displays images from Sharepoint using the
I have an android application i have been working on that downloads an image
We have a C# application that connects to a FTP server, downloads some files,
I'm working on some code that generates an Excel spreadsheet server-side and then downloads
I'm working on an ipad application that needs to work with images; it uses
I am working on splitting out an existing, working application that I currently have
Im working on an application that needs to talk to a database. The application
I'm working on an application that allows users to edit invoices generated in other

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.