I'd like to know how I can download an Image from a given URL and display it inside an ImageView. And is there any permissions
required to mention in the manifest.xml
file?
-
You must go thru this simply. http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html#WorkerThreads – Kartik Domadiya Oct 14 '11 at 06:49
-
Check this nice tutorial for beginner: [Connecting to the Web: I/O Programming in Android](http://www.devx.com/wireless/Article/39810/1954). – Nibha Jain Oct 14 '11 at 06:36
5 Answers
You need to put this permission
to access the Internet
<uses-permission android:name="android.permission.INTERNET" />
you can try this code.
String imageurl = "YOUR URL";
InputStream in = null;
try
{
Log.i("URL", imageurl);
URL url = new URL(imageurl);
URLConnection urlConn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.connect();
in = httpConn.getInputStream();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
Bitmap bmpimg = BitmapFactory.decodeStream(in);
ImageView iv = "YOUR IMAGE VIEW";
iv.setImageBitmap(bmpimg);
Use background thread to get image and after getting image set it in imageview using hanndler.
new Thread(){
public void run() {
try {
Bitmap bitmap = BitmapFactory.decodeStream(new URL("http://imageurl").openStream());
Message msg = new Message();
msg.obj = bitmap;
imageHandler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
Handler code where we set downloaded image in imgaeview.
Handler imageHandler = new Handler(){
public void handleMessage(Message msg) {
if(msg.obj!=null && msg.obj instanceof Bitmap){
imageview.setBackgroundDrawable(new BitmapDrawable((Bitmap)msg.obj));
}
};
};
And ofcourse you need internet permission.
<uses-permission android:name="android.permission.INTERNET" />

- 3,894
- 25
- 39

- 6,263
- 7
- 52
- 86
You need to set usage
permission of INTERNET
in android manifest file and use java.net.URL
and java.net.URLConnection
classes to request the URL.

- 93,659
- 19
- 148
- 186
There is the BitmapFactory
-class which can do that. It can create a Bitmap
-object from an InputStream
, which can then be displayed in an ImageView
.
Something you should know is, that using a normal URLConnection
to get an InputStream
on a URL
-object does not always work with the bitmap-factory. A work-around is presented here: Android: Bug with ThreadSafeClientConnManager downloading images

- 1
- 1

- 25,449
- 15
- 83
- 111
look this:
Option A:
public static Bitmap getBitmap(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
bis.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bm;
}
Option B:
public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
input.close();
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Jus run the method in a background thread.

- 1,812
- 1
- 25
- 26