Created
July 1, 2010 01:31
-
-
Save mapiondev/459428 to your computer and use it in GitHub Desktop.
[java][android] 外部画像取得
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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