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

  • Home
  • SEARCH
  • 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 957893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:46:48+00:00 2026-05-16T00:46:48+00:00

I’m using BitmapFactory.decodeStream to load an image from a url in Android. I want

  • 0

I’m using BitmapFactory.decodeStream to load an image from a url in Android. I want to only download images below a certain size, and I’m currently using getContentLength to check this.

However, I’m told that getContentLength doesn’t always provide the size of the file, and in those cases I would like to stop the download as soon as I know that the file is too big. What is the right way to do this?

Here is my current code. I currently return null if getContentLength doesn’t provide an answer.

HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
final long contentLength = bufHttpEntity.getContentLength();
if ((contentLength >= 0 && (maxLength == 0 || contentLength < maxLength))) {
    InputStream is = bufHttpEntity.getContent();
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    return new BitmapDrawable(bitmap);
} else {
    return null;
}
  • 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-16T00:46:49+00:00Added an answer on May 16, 2026 at 12:46 am

    You should use HttpHead to issue a HEAD request, which is similar to GET but will return only headers. If the content length is satisfactory, then you can make your GET request to get the content.

    The majority of servers should handle HEAD requests without a problem, but occasionally an application servers won’t be expecting it and will throw an error. Just something to be aware of.

    UPDATE thought I would try to answer your actual question as well. You will probably have to read the data into an intermediate byte buffer before passing the data to the BitmapFactory.

    InputStream is = bufHttpEntity.getContent();
    ByteArrayOutputStream bytes = ByteArrayOutputStream();
    byte[] buffer = new byte[128];
    int read;
    int totalRead = 0;
    while ((read = is.read(buffer)) > 0) {
       totalRead += read;
       if (totalRead > TOO_BIG) {
         // abort download. close connection
         return null;
       }
       bytes.write(buffer, 0 read);
    }
    

    Unrelated, but remember to always call consumeContent() on the entity after use so that HttpClient can reuse the connection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.