The Overview
I’ll make my usual yammering quick so you can get straight to the good stuff. This is just a snippet to show you how you can display an image from the Internet in your native Android app via ImageView. This is ideal in a few situations, namely when you need to keep app size and initial load time down, or if it isn’t feasible to locally store all the images your app will use.
The Code
package com.savagelook; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.ImageView; public class ShowImageActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // YOUR_LAYOUT is the name of your layout resource // IMAGEVIEW_ID is your ImageView in YOUR_LAYOUT setContentView(R.layout.YOUR_LAYOUT); ImageView imageView = (ImageView)findViewById(R.id.IMAGEVIEW_ID); imageView.setImageDrawable(createDrawableFromURL("https://savagelook.com/misc/sl_drop2.png")); } private Drawable createDrawableFromURL(String urlString) { Drawable image = null; try { URL url = new URL(urlString); InputStream is = (InputStream)url.getContent(); image = Drawable.createFromStream(is, "src"); } catch (MalformedURLException e) { // handle URL exception image = null; } catch (IOException e) { // handle InputStream exception image = null; } return image; } }
Great explaining, just what i needed.
Image still not loading. no error messgaes no nothing. it jus does not show. i even tried . still nothing.
got it working. it seems like size of the image was a problem
Thanks, was very helpful.