【问题标题】:Interrupted Image Download中断的图像下载
【发布时间】:2014-07-30 11:17:30
【问题描述】:

我正在使用我在 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;
}

连接丢失时我可以控制,但是当连接速度慢导致图像无法完全下载时,我没有任何控制。

如何查看图片是否未完全下载?

【问题讨论】:

  • 使用异步任务下载图片。它会对你有很大帮助。
  • 是的,该方法在异步任务中调用。我更新了问题:)
  • @hungr 不,它不是重复的。我的问题是,我认为它是由于连接速度慢而断开连接。这导致未完成的图像。但它不会丢弃任何错误。

标签: android androidhttpclient


【解决方案1】:

在使用之前试试这个:Drawable.createFromPath(filePath)!=​​ null

【讨论】:

  • 我不明白。这个怎么用?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 2010-10-27
    相关资源
    最近更新 更多