2011-11-10 29 views
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; 
} 

回答

0

我從網址獲取時使用的 流讀者緩衝的變型,當運行到這一點。

對我而言,簡單的解決方案是使用BufferedHttpEntity獲取圖像數據。

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

return BitmapFactory.decodeStream(in,null,decodeOptions); 此代碼不允許BufferedReader類。你確定你對於android – ericlee

+0

你好我可能知道什麼是inPreferredConfig – ericlee

+0

基本上你想要什麼顏色深度解碼爲。 http://developer.android.com/reference/android/graphics/Bitmap.Config.html BitmapConfig.ARGB_888是最常見的...但重。 BitmapConfig.RGB_565是妥協妥協 – mmeyer