开发者

Trying to display images from a website in Android

开发者 https://www.devze.com 2023-02-04 05:45 出处:网络
below is my code... but only a blank screen shows up, anyone know what\'s up? public void onCreate(Bun开发者_如何学Pythondle savedInstanceState) {

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

public void onCreate(Bun开发者_如何学Pythondle 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();
        }
    }


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消