0

http://android-developers.blogspot.gr/2010/07/multithreading-for-performance.htmlのコードを http://developer.android.com/training/displaying-bitmaps/index と組み合わせて使用​​しています。 htmlコード。サンプルサイズを変更せずに非同期でURLから画像をダウンロードしようとすると、すべて問題ありません。しかし、サンプルサイズを計算しようとすると、画面(グリッドビュー)に何も表示されません。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);
}

最初と 2 番目の BitmapFactory.decodeStream の両方を使用すると問題が発生します。2番目のものだけを使用する場合はすべて問題ありませんが、実際にはサンプルを作成していません。なにか提案を?私はそれを探すのに多くの時間を失いました。

4

2 に答える 2

2

InputStream は 1 回しか読み取れず、その後は失われます。

デュアル パスを作成する場合 (1 つは境界のみ、もう 1 つはオプションを持つため、最初に入力ストリームを一時ファイルにコピーし (FileOutputStream を使用)、次に 2 つのパスを開いてそのファイルでデュアル パスを実行する必要があります)。 FileInputStream.

于 2013-01-11T15:52:06.453 に答える
0

または、同じ試行でclient.execute()を2回実行することもできます。最初にサンプルサイズを決定し、2番目に正しいビットマップを作成します。このようにして、ファイル全体を保存する必要はありません。最初のInputstreamをすぐに閉じます。

于 2013-01-11T19:49:01.080 に答える