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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:38:28+00:00 2026-05-26T19:38:28+00:00

I am using the code below in asyn task to download a bitmap to

  • 0

I am using the code below in asyn task to download a bitmap to be added to my custom class. however sometimes it return nulls with no IOException or any exception. i am not very sure what can be done

    public  Bitmap downloadFile(String fileUrl){
    URL myFileUrl =null;          
    try {
        myFileUrl= new URL(fileUrl);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
        conn.setDoInput(true);
        //conn.setReadTimeout(500000000);
        conn.connect();
        InputStream is = conn.getInputStream();
           BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, o);
        is.close();
        conn.disconnect();
        int scale = 1;
        int IMAGE_MAX_SIZE=400;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }
        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        conn= (HttpURLConnection)myFileUrl.openConnection();
        conn.setDoInput(true);
        //conn.setReadTimeout(500000000);
        conn.connect();
        is  =  conn.getInputStream();
       Bitmap b = BitmapFactory.decodeStream(is, null, o2);
       if (b==null)
           Log.e(Config.log_id, " Download image failed");
    return b;

    }

    catch (IOException e) {
        Log.e(Config.log_id, " Download image failed"+e.getMessage());
        e.printStackTrace();
    }
    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-26T19:38:28+00:00Added an answer on May 26, 2026 at 7:38 pm

    I have run into this as well when using unbuffered variants of
    stream readers when getting from urls.

    The simple solution that worked for me was to use a BufferedHttpEntity to get the image data.

    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.entity.BufferedHttpEntity;
    
    public static Bitmap decodeFromUrl(HttpClient client, URL url, Config bitmapCOnfig)
        {
            HttpResponse response=null;
            Bitmap b=null;
            InputStream instream=null;
    
            BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
            decodeOptions.inPreferredConfig = bitmapCOnfig;
            try
            {
                HttpGet request = new HttpGet(url.toURI());
                response = client.execute(request);
                if (response.getStatusLine().getStatusCode() != 200)
                {
                    MyLogger.w("Bad response on " + url.toString());
                    MyLogger.w ("http response: " + response.getStatusLine().toString());
                    return null;
                }
                BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(response.getEntity());
                instream = bufHttpEntity.getContent();
    
                return BitmapFactory.decodeStream(instream, null, decodeOptions);
            }
            catch (Exception ex)
            {
                MyLogger.e("error decoding bitmap from:" + url, ex);
                if (response != null)
                {
                    MyLogger.e("http status: " + response.getStatusLine().getStatusCode());
                }
                return null;
            }
            finally
            {
                if (instream != null)
                {
                    try {
                        instream.close();
                    } catch (IOException e) {
                        MyLogger.e("error closing stream", e);
                    }
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

C# 2008 SP1 I am using the code below: dt.ReadXml(%AppData%\\DateLinks.xml); However, I am getting
Using the code below each image download) file_get_contents() ) takes on average 8-15 seconds.....
I am using code below, the session variables are declarated in Common class and
I am trying to grow a database using the following the code below. However
jqGrid add forms contains autocomplete boxes using code below. If new row is added
My ASP.NET application return PDF file to user using code below Context.Response.Clear(); Context.Response.ContentType =
Using the code below, I am returning an nvarchar field from MS SQL 2005
Using the code below, I am attempting to fill a Canvas with UIElements and
Using the code below (from a console app I've cobbled together), I add seven
I am using the code below to save and restore the window position and

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.