[Android] Get Bitmap from URL

  • 0
* Ref: Android load from URL to Bitmap


public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
我還沒有測試如果取的是 https (如 Facebook)是否要用 HttpsURLConnection, 我自己的實作是有區分的

沒有留言 :

張貼留言