get Picture from URL - Android Network

Android examples for Network:URL

Description

get Picture from URL

Demo Code


//package com.java2s;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {

    public static Bitmap getPic(String Url) throws Exception {
        URL url = new URL(Url);//URL
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();//
        conn.setConnectTimeout(10000);//
        conn.setRequestMethod("GET");//
        int responseCode = conn.getResponseCode();
        if (responseCode == 200) {//200
            InputStream inputStream = conn.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);//
            inputStream.close();//from  w ww  .j  ava 2s  . c  o  m
            return bitmap;

        }
        return null;

    }
}

Related Tutorials