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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:16:12+00:00 2026-06-12T13:16:12+00:00

Given the image url, I need to download it and display in an ImageView.

  • 0

Given the image url, I need to download it and display in an ImageView.
Everything works fine, except the situation when the images are quite large, and an OutOfMemoryException is thrown.

Hopefully, Android documentation provides a solution to this problem: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

I tried to adapt that snipped of code to accept an InputStream, instead of a Resource.
However, it seems I am missing something there, because the image is not displayed, no exception, but I see in the LogCat this: SkImageDecoder::Factory returned null

Here is how I decode the image and scale it down (based on Android documentation):

public static Bitmap decodeBitmapFromInputStream(InputStream inputStream,
            int reqWidth, int reqHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(inputStream, null, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(inputStream, null, options);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
    }

Now, in my bitmap downloader method, I call it like this:

inputStream=entity.getContent();
return decodeBitmapFromInputStream(inputStream, 320, 480);

Here’s the full method, just in case:

static Bitmap downloadBitmap(String url){
        AndroidHttpClient client=AndroidHttpClient.newInstance("Android");
        HttpGet getRequest=new HttpGet(url);

        try{
            HttpResponse response=client.execute(getRequest);
            int statusCode=response.getStatusLine().getStatusCode();

            if(statusCode!=HttpStatus.SC_OK){
                Log.d("GREC", "Error "+statusCode+" while retrieving bitmap from "+url);
                return null;
            }

            HttpEntity entity=response.getEntity();
            if(entity!=null){
                InputStream inputStream=null;
                try{
                    inputStream=entity.getContent();
                    return decodeBitmapFromInputStream(inputStream, 320, 480);
                }catch (Exception e) {
                    Log.d("GREC", "Exception occured in BitmapDownloader");
                    e.printStackTrace();
                }
                finally{
                    if(inputStream!=null){
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        }catch (Exception e) {
            getRequest.abort();
            Log.d("GREC", "Error while retriving bitmap from "+url+", "+e.toString());
        }finally{
            if(client!=null){
                client.close();
            }
        }
        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-06-12T13:16:14+00:00Added an answer on June 12, 2026 at 1:16 pm

    The problem was that once you’ve used an InputStream from a HttpUrlConnection, you can’t rewind and use the same InputStream again. Therefore you have to create a new InputStream for the actual sampling of the image. Otherwise we have to abort the http request.
    see decodeStream returns null

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

Sidebar

Related Questions

Given a url to an image is there a way in Django/Python to pull
Given a URL to an image (and not the image itself), what's the most
i am using asynctask to fetch images from given url.these images are displaying in
I need to upload a given image using Amazon S3 I have this PHP:
I have grid of images, when the mouse is over any given image a
I need to create a html parser, that given a blog url, it returns
I have 4000 images on my amazon s3. Now i need to download 3000
In my webgrid I need to display images based on the value .. Code
Given the following CSS rule, #block1 { text-indent: -1000em; background: transparent url(../images/xxx.png) no-repeat scroll
Why does this: URL url = MinecraftPlatformGame.class.getResource(images/diamondPick.png); image = Toolkit.getDefaultToolkit().getImage(url); int width = image.getWidth(null);

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.