gridview异步下载

时间:2013-01-11 15:48:07

标签: android multithreading gridview

我将http://android-developers.blogspot.gr/2010/07/multithreading-for-performance.html的代码与http://developer.android.com/training/displaying-bitmaps/index.html代码结合使用。当我尝试异步下载网址中的图像而不更改样本大小一切都没问题。但是,当我尝试计算样本大小时,屏幕上没有任何内容(gridview)。我读了logcat,我看到所有图像都正确下载。我用于图像下载的代码是下一个:

Bitmap downloadBitmap(String url) {

    final HttpClient 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) {
            Log.w("ImageDownloader", "Error " + statusCode
                    + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();

                // get Device Screen Dimensions
                DeviceProperties dimensions = new DeviceProperties(activity);

                return 
                decodeBitmapFromResource(url, inputStream,
                        dimensions.getDeviceWidth(),
                        dimensions.getDeviceHeight());
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        Log.w(TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        Log.w(TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        Log.w(TAG, "Error while retrieving bitmap from " + url, e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}

我用于解码的代码是:

public static Bitmap decodeBitmapFromResource(String url, InputStream is,
        int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();

    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(new FlushedInputStream(is), null,
            options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options , reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(new FlushedInputStream(is), null,
            options);
}

当我使用第一个和第二个BitmapFactory.decodeStream时出现问题。如果我只使用第二个一切都没关系,但实际上我没有做任何样品。有什么建议吗?我已经失去了很多时间寻找它。

2 个答案:

答案 0 :(得分:2)

只能读取一个InputStream,然后它就会消失。

如果你想进行双遍(一个只用于边界,第二个用于选择,你必须先将输入流复制到一个临时文件(使用FileOutputStream),然后对该文件进行双重传递通过打开两个FileInputStream。

答案 1 :(得分:0)

或者,您可以在同一次尝试中执行两次client.execute()。第一个确定samplesize,第二个确定正确的位图。这样你就不必存储整个文件。立即关闭第一个Inputstream。