1. Check out our companion app, Forums for Android! Download from Google Play

How to download and display images?

Discussion in 'Application Development' started by mferrell, Jun 30, 2010.

  1. mferrell

    mferrell New Member
    Thread Starter
    5

    Jun 30, 2010
    1
    0
    5
    Hello. I'm trying to make an app in which I download some images, and then display then. From my understanding, images to be displayed need to be in the drawable folder and R.java, but I can't update the latter while the app is running.

    Any idea on how to go about adding images that I get while the app is in use?

    Thanks.
     

    Advertisement

  2. You need to download the image and then create a drawable with the data.

    Code (Text):
    1. URL url = new URL(link);
    2. URLConnection connection = url.openConnection();
    3. connection.connect();
    4. InputStream is = connection.getInputStream();
    5. BufferedInputStream bis = new BufferedInputStream(is);
    6. Bitmap bitmap = BitmapFactory.decodeStream(bis);
    7. bis.close();
    8. is.close();
    Obviously, you'll want to wrap that in a try/catch and run it on a thread other than the main UI thread. Create and findViewById an ImageView and call it's setImageBitmap method with the new bitmap you have created.
     
  3. jenni07

    jenni07 New Member
    5

    Sep 9, 2011
    1
    0
    5
    I am trying to use the above code... but it is giving me request time out... connection.connect() tis line is taking too much time... i'm getting the following error.. can u help me with wat modifications i can do to get images from a url and display it in imageview?
    08-25 16:46:51.404: DEBUG/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
    08-25 16:47:19.616: WARN/System.err(7344): java.net.SocketException: The operation timed out
    08-25 16:47:19.625: WARN/System.err(7344): at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
    08-25 16:47:19.634: WARN/System.err(7344): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
    08-25 16:47:19.634: WARN/System.err(7344): at org.apache.harmony.luni.net.PlainSocketImpl.connect
     
  4. kbbaloch

    kbbaloch New Member
    5

    May 23, 2012
    1
    0
    5
    hi dear
    please check ip which you have given....
     

Share This Page

Loading...