質問

サーバーからリモート画像をロードしようとします。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は常に有効な値を返し、写真がサーバーに存在することを知っています。

最高の願い、ダニエル

役に立ちましたか?

解決

答えを見つけました ここ フェッチメソッドを次のように更新しました。

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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top