我正在使用以下功能将远程图像加载到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);
}
答案 0 :(得分:0)
使用https://github.com/kaeppler/ignition可能是一个选项,尝试使用RemoteImageView,它非常容易使用。
答案 1 :(得分: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);
}
注意:它同步加载。如果它工作,你的下一步应该加载一个线程,不要阻塞主线程。
答案 2 :(得分:0)
我使用Prime来加载所有图片。它可以轻松应对。
答案 3 :(得分: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) {
}