Tuesday, July 17, 2012

Android - Function to convert ImageURL to Bitmap object


IMAGE URL TO BITMAP

Use this function “convertURLtoBitmap”

public Bitmap convertURLtoBitmap(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;

                }
}