---
title: "android如何获取url指定的图片资源"
author: "kylines"
date: "2012-08-23T18:19:36Z"
tags: []
categories: ["移动开发"]
slug: "android-url"
draft: false
lastmod: "2012-08-23T18:18:34Z"
id: 1660180
url: "https://kylines.iteye.com/blog/1660180"
---

```java
// 获取指定路径的图片
	public static Bitmap getImage(String urlpath)
			throws Exception {
		URL url = new URL(urlpath);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(5 * 1000);
		Bitmap bitmap = null;
		if (conn.getResponseCode() == 200) {
			InputStream inputStream = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(inputStream);
		}
		return bitmap;
	}
```