我使用以下函數加載一個遠程圖像到ImageView。圖像存在於URL,我想從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);
}
通過下面的代碼能夠加載遠程圖像到ImageView:
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) {
}