• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to download and display images?

mferrell

Lurker
Jun 30, 2010
1
0
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.
 
You need to download the image and then create a drawable with the data.

Code:
URL url = new URL(link);
URLConnection connection = url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
bis.close();
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.
 
Upvote 0
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
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones