中断图像下载

时间:2014-07-10 06:14:49

标签: android androidhttpclient

我正在使用此方法下载位图,我在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;
}

我可以控制连接丢失的情况,但是,当连接速度很慢导致图像无法完全下载时,我无法控制。

如何检查图像是否未完全下载?

2 个答案:

答案 0 :(得分:0)

尝试这个:Drawable.createFromPath(filePath)!= null,然后再使用它

答案 1 :(得分:0)

我的解决方案是将图像文件名放在变量中,并从流和下载的文件中检查文件大小。如果它不匹配,我删除该文件。