0

I have some code that retrieves an image from the web using this method

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 options=new BitmapFactory.Options();
        options.inSampleSize = 8;

        return BitmapFactory.decodeStream(is,null,options);

    } catch (IOException e) {
        Log.e("log_tag", " Download image failed"+e.getMessage());
        e.printStackTrace();
    }
    return null;
}

However, sometimes it returns null with no error, and sometimes it works.

I tried setting the timeout to a large number and it still doesn't have an error.

coder
  • 10,460
  • 17
  • 72
  • 125
ericlee
  • 2,703
  • 11
  • 43
  • 68
  • It seems like the only way for that code to return null is if there is an io exception, do you see a stack trace? – Nick Campion Nov 09 '11 at 18:39
  • I did a try and catch on exception but it doesn't return any error just returned null – ericlee Nov 09 '11 at 18:54
  • http://stackoverflow.com/questions/3802820/bitmapfactory-decodestream-always-returns-null-and-skia-decoder-shows-decode-ret seems to be a bug – ericlee Nov 09 '11 at 18:56
  • Looks like a bug.. I am dealing with the same challenge when trying to retrieve location geopoints from Google map service – apesa Nov 10 '11 at 18:35

1 Answers1

1

Try this bit of code in your catch clause. It will display a toast msg with the error. You are probably not getting out all the time.

catch(IOException e) {
Log.e("IOException", e.getMessage());

//Toaster on high-----------------//

Context context = getApplicationContext();
CharSequence text = "IOException:  " + e.getMessage();
int dur = Toast.LENGTH_LONG;
Toast.makeText(context, text, dur).show();
apesa
  • 12,163
  • 6
  • 38
  • 43