8

サーバーからリモートイメージをロードしようとしていますが、stackoverflowの多くのコード例のおかげで、3つのイメージのうち2つで機能するソリューションがあります。3番目の画像の問題が何であるかはよくわかりません。デバッガーでコードを実行すると、画像が読み込まれることがあります。また、問題のある画像を最初にロードすると、他の2つの画像がロードされないことがあります。

コードは次のとおりです。

public static Drawable getPictureFromURL(Context ctx, String url, final int REQUIRED_SIZE) throws NullPointerException {
    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    int scale = 1;
    if (o.outWidth > REQUIRED_SIZE) {
        scale = (int) Math.pow(2, (int) Math.round(Math.log(REQUIRED_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
    }
    Log.i(Prototype.TAG, "scale: "+scale); 

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    Bitmap bmp;
    try {
        bmp = BitmapFactory.decodeStream((InputStream) Tools.fetch(url), null, o2);
        if(bmp!=null)
            return new BitmapDrawable(ctx.getResources(), bmp);
        else
            return null;
    } catch (Exception e) {
        Log.e(Prototype.TAG, "Exception while decoding stream", e);
        return null;
    }
}

デバッグ中に、o.outWidthが-1であり、エラーを示していることがわかりましたが、例外はスローされないため、何が問題だったのか実際にはわかりません。InputStreamは常に有効な値を返しました、そして私は絵がサーバーに存在することを知っています。

よろしくお願いします、ダニエル

4

1 に答える 1

16

ここで答えを見つけ、fetch メソッドを次のように更新しました。

private static InputStream fetch(String address) throws MalformedURLException,IOException {
    HttpGet httpRequest = new HttpGet(URI.create(address) );
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    InputStream instream = bufHttpEntity.getContent();
    return instream;
}
于 2010-12-11T13:01:28.757 に答える