1
我在asyn任務中使用下面的代碼來下載要添加到我的自定義類的位圖。然而有時它會返回沒有IOException或任何異常的空值。我不是很知道什麼可以做Android的位圖下載有時會返回null
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;
}
return BitmapFactory.decodeStream(in,null,decodeOptions); 此代碼不允許BufferedReader類。你確定你對於android – ericlee
你好我可能知道什麼是inPreferredConfig – ericlee
基本上你想要什麼顏色深度解碼爲。 http://developer.android.com/reference/android/graphics/Bitmap.Config.html BitmapConfig.ARGB_888是最常見的...但重。 BitmapConfig.RGB_565是妥協妥協 – mmeyer