5

私はAndroidアプリケーションを開発していますが、そのビューには複数のギャラリーが含まれています。ギャラリー(ビットマップ)のコンテンツはインターネットから赤く表示されます。

最初のギャラリーではすべて正常に機能しますが、2番目のギャラリーの最初の画像をダウンロードしようとするとBitmapFactory.decodeStream(InputStream)nullが返されますが、ストリームはnullではありません。

public void loadBitmap() throws IOException {

        for (int i = 0; i < images.size(); ++i) {
            URL ulrn = new URL(images.get(i).getThumbUrl());
            HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
            InputStream is = con.getInputStream();
            images.get(i).setImage(BitmapFactory.decodeStream(is));
            Log.i("MY_TAG", "Height: " + images.get(i).getImage().getHeight());
        }
}

getThumbUrl()は画像のURL(例:http://mydomain.com/image.jpg )を返し、そのNullPointerException行にaをスローしますLog.i("MY_TAG", "Height: ... ) (これimagesArrayList私のクラスのオブジェクトを含み、URLとビットマップも保持します)。

アドバイスありがとうございます!

4

3 に答える 3

7

私はこれに遭遇しました。入力ストリームで BufferedHttpEntity を使用してみてください。これにより、decodeStream からサイレント ヌルを取得する際の問題の 99.9% が防止されることがわかりました。

重要ではないかもしれませんが、次のように HttpURLConnection ではなく org.apache.http.client.HttpClient を確実に使用します。

public static Bitmap decodeFromUrl(HttpClient client, URL url, Config bitmapCOnfig)
{
    HttpResponse response=null;
    Bitmap b=null;
    InputStream instream=null;

    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    decodeOptions.inPreferredConfig = bitmapCOnfig;
    try
    {
    HttpGet request = new HttpGet(url.toURI());
        response = client.execute(request);
        if (response.getStatusLine().getStatusCode() != 200)
        {
            MyLogger.w("Bad response on " + url.toString());
            MyLogger.w ("http response: " + response.getStatusLine().toString());
            return null;
        }
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(response.getEntity());
        instream = bufHttpEntity.getContent();

        return BitmapFactory.decodeStream(instream, null, decodeOptions);
    }
    catch (Exception ex)
    {
        MyLogger.e("error decoding bitmap from:" + url, ex);
        if (response != null)
        {
            MyLogger.e("http status: " + response.getStatusLine().getStatusCode());
        }
        return null;
    }
    finally
    {
        if (instream != null)
        {
            try {
                instream.close();
            } catch (IOException e) {
                MyLogger.e("error closing stream", e);
            }
        }
    }
}
于 2011-05-09T19:26:30.683 に答える
1

Google が私をここに連れてきました。同じ問題を抱えているすべての人のために:

問題: http://code.google.com/p/android/issues/detail?id=6066

ソリューション (「FlushedInputStream」): http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

于 2011-10-06T08:06:52.680 に答える
-1
public static Bitmap decodeStream (InputStream is)

戻り値

デコードされたビットマップ、またはイメージ データをデコードできなかった場合は null。

画像の代わりに 404 エラーまたは類似のエラーが表示されていないことを確認しましたか?

于 2011-05-09T18:58:10.577 に答える