2014-07-10 76 views
0

我使用這個方法,我在裏面的AsyncTask調用下載位圖:中斷的圖片下載

public static Bitmap downloadBitmap(String url) { 
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); 
    final HttpGet getRequest = new HttpGet(url); 
    try { 
     HttpResponse response = client.execute(getRequest); 
     final int statusCode = response.getStatusLine().getStatusCode(); 
     if (statusCode != HttpStatus.SC_OK) { 
      return null; 
     } 
     final HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream inputStream = null; 
      try { 
       System.setProperty("http.keepAlive", "false"); 
       inputStream = entity.getContent(); 
       final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
       return bitmap; 
      } finally { 
       if (inputStream != null) { 
        inputStream.close(); 
       } 
       entity.consumeContent(); 
      } 
     } 
    } catch (Exception e) { 
     getRequest.abort(); 
    } finally { 
     if (client != null) 
      client.close(); 
    } 
    return null; 
} 

我有當連接丟失的控制,但是,我沒有任何當連接速度慢引起圖像不完全下載。

如何檢查,如果圖像沒有完全下載?

+0

使用異步任務下載圖像。它會幫助你很多。 –

+0

是的,這個方法在asynctask裏面調用。我更新了問題:) –

+0

可能的重複[如何檢查下載映像的進程是否完成?](http://stackoverflow.com/questions/13965294/how-to-check-whether-the-process -of-download-image-is-completed-or-not) – hungr

回答

0

我的解決方案之前,空是把圖像文件名中的一個變量,檢查從流文件的大小和下載的文件。如果它不匹配,我刪除該文件。

0

試試這個:Drawable.createFromPath(文件路徑)=用它

+0

我不明白。我如何使用這個? –