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

它可能是使用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 回答
0

我使用Prime来加载所有图像。它可以轻松处理这个问题。

于 2012-06-21T20:23:45.573 回答
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 回答