Alex
Alex

Reputation: 21

Trying to display images from a website in Android

below is my code... but only a blank screen shows up, anyone know what's up?

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        URL url;
        try {
            url = new URL("http://pennapps.com/biblioteka/images/C.jpg");
            URLConnection conn=url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
            ImageView image = new ImageView(this);
            image.setImageBitmap(bm);
            setContentView(image);
        } 



        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Upvotes: 2

Views: 1785

Answers (2)

Parth Chandan
Parth Chandan

Reputation: 1

You forgot the permission tag , else the code seems to be working

Upvotes: 0

Ian G. Clifton
Ian G. Clifton

Reputation: 9439

Without seeing the logs, it's tough to say, but a common pitfall is forgetting to request the INTERNET permission. In addition, it's highly recommended that you do not make web requests on the main (UI) thread. There is an excellent article on Multithreading for Performance that also covers the topic of image downloading.

Upvotes: 2

Related Questions