0

次の関数を使用して、リモート画像を ImageView に読み込みます。画像は、ロードしようとしている URL に確実に存在しますが、関数を呼び出して画像を表示しようとすると、ImageView は空白のままになります。

public static void setImage(ImageView view, String url) throws IOException {
    final URLConnection conn = new URL(url).openConnection();
    conn.connect();
    final InputStream is = conn.getInputStream();

    final BufferedInputStream bis = new BufferedInputStream(is, 100000);

    final Bitmap bm = BitmapFactory.decodeStream(bis);
    bis.close();
    is.close();

    view.setImageBitmap(bm);
}
4

4 に答える 4

0

これは私のために働いた:

    Bitmap bitmap = null;

    HttpGet httpRequest = null;


    try {
            URL url = new URL(your_url);
            httpRequest = new HttpGet(url.toURI());

            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

            HttpEntity entity = response.getEntity();
            BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
            InputStream instream = bufHttpEntity.getContent();
            bitmap = BitmapFactory.decodeStream(instream);
    } catch (URISyntaxException e) {
            e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
    }
于 2012-06-21T20:24:56.560 に答える
0

私はすべての画像の読み込みにPrimeを使用しています。これは簡単に処理できます。

于 2012-06-21T20:23:45.573 に答える
0

https://github.com/kaeppler/ignitionを使用するオプションかもしれません。RemoteImageView を試してみてください。非常に使いやすいです。

于 2012-06-21T20:01:09.450 に答える
0

これを試して

private void setImage(String urlStr, ImageView iv) throws ClientProtocolException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(urlStr);
    HttpResponse response = httpClient.execute(request);
    InputStream is = response.getEntity().getContent();
    TypedValue typedValue = new TypedValue();
    typedValue.density = TypedValue.DENSITY_NONE;
    Drawable drawable = Drawable.createFromResourceStream(null, typedValue, is, "src");
    iv.setImageDrawable(drawable);
}

注: 同期的にロードされます。それが機能する場合は、メイン スレッドをブロックしないように、次のステップをスレッドにロードする必要があります。

于 2012-06-21T20:12:56.697 に答える