如何从android中的特定网址读取图片?
答案 0 :(得分:11)
使用此
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(img );
答案 1 :(得分:5)
下面的代码应该可以帮助您阅读图像。但请记住,如果你在UI线程中执行此操作,那么它会挂起UI。您应该始终打开一个新线程并在该线程中加载图像。因此,您的应用始终保持响应。
InputStream is = null;
BufferedInputStream bis = null;
Bitmap bmp = null;
try {
URLConnection conn = url.openConnection();
conn.connect();
is = conn.getInputStream();
bis = new BufferedInputStream( is );
bmp = BitmapFactory.decodeStream( bis );
} catch (MalformedURLException e) {
} catch (IOException e) {
} finally {
try {
is.close();
bis.close();
} catch (IOException e) {
}
}
imageView.setImageBitmap( bmp );