Skip to content

Instantly share code, notes, and snippets.

@mapiondev
Created July 1, 2010 01:31
Show Gist options
  • Save mapiondev/459428 to your computer and use it in GitHub Desktop.
Save mapiondev/459428 to your computer and use it in GitHub Desktop.
[java][android] 外部画像取得
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public final class HttpClient {
private HttpClient() {
}
public static Bitmap getBitmap(String urlStr) {
URL url = null;
try {
url = new URL(urlStr);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return getBitmap(url);
}
public static Bitmap getBitmap(URL url) {
HttpURLConnection con = null;
InputStream in = null;
try {
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
in = con.getInputStream();
return BitmapFactory.decodeStream(in);
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.disconnect();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment