如何发出 http 请求以获取图像链接: http: //vec03.maps.yandex.ru/tiles ?l=map&v=2.20.0&x=19783&y=10320&z=15 以便能够添加到 ImageView
请帮忙,我不擅长网络开发。
如何发出 http 请求以获取图像链接: http: //vec03.maps.yandex.ru/tiles ?l=map&v=2.20.0&x=19783&y=10320&z=15 以便能够添加到 ImageView
请帮忙,我不擅长网络开发。
String imageUrl= "http://vec03.maps.yandex.ru/tiles?l=map&v=2.20.0&x=19783&y=10320&z=15";
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(img );
在你用 SO 提出问题之前先谷歌一下,避免重复的问题
public void setImage()
{
ImageView icon=(ImageView)v.findViewById(R.id.icon);
Drawable image = ImageOperations(getapplicationContext(), "yuour url",
"image.jpg");
icon.setImageDrawable(image);
}
private Drawable ImageOperations(Context ctx, String url,
String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}