【发布时间】:2025-12-06 16:15:02
【问题描述】:
如何从 android 中的特定 url 读取图像?
【问题讨论】:
-
参考this page.... 会很有帮助的..快乐编码...!!!!
如何从 android 中的特定 url 读取图像?
【问题讨论】:
下面的代码应该可以帮助您阅读图像。但是请记住,如果您在 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 );
【讨论】:
使用这个
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(img );
【讨论】: