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;
}
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.