Android Load Image From URL Example

In android we can show image from URL (web).
Here we are going to see about how to load the image from web in simple way.
Example for Android Load Image From Web :-
Add the below code to AndroidManifest.xml for Internet Permission.
[sourcecode language="xml"]
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
[/sourcecode]
Edit your main.xml file
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/ImageView01"
android:layout_height="wrap_content" android:layout_width="wrap_content"/>
</LinearLayout>
[/sourcecode]
Edit your java file
[sourcecode language="java"]
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class ImageFromUrlExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
imgView.setImageDrawable(drawable);

}

private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
[/sourcecode]
output will looks like

Comments

What error you got Ron ?

What error you got Ron ?

Hi Ron, If you like to add

Hi Ron,

If you like to add URL example to gallery Example,(as you mentioned in gallery example comment ) you need to do,

First save all the 30 photos URL in a String Array.

See the Image URL Example 16th line ( Drawable drawable = LoadImageFromWebOperations(&rdquo ;http://www.androidpeople.com/wp-content/uploads/2010/03/android.png”); )

In this we are getting only one drawable. For your program you need 30 drawable.

So create Drawable as Array. And then send the link to LoadImageFromWebOperations() using ’for’ loop for 30 times. Now you will get 30 drawables images in array.

Now see the Gallery example 72nd line ( imgView.setBackgroundResource(GalItemBg); ).

Change that to imagView.setImageDrawable(drawable[arraynumber]);

That’s all.

Now you will get 30 images to Gallery from URL.

Thanks for your code strengis

Thanks for your code strengis :)
To load multiple image in background process it will be very useful.

@Alios, If you get "Unknown

@Alios,

If you get "Unknown Host Exception" then its problem with your network.