April 20, 2011

How to load an image from Web on Android

Find below complete code for a simple application that loads an image from web. You have to insert the url and click the button. You can use this example in your applications when you need to load images from web.
Example:






Add this line in your AndroidManifest.xml file:


Layout main.xml


 
 
 


main.java
package com.blog.loadimage;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class main extends Activity {
 
 EditText imageUrl;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);         
        imageUrl = (EditText)findViewById(R.id.editTextUrl);
  Button getImageButton = (Button)findViewById(R.id.buttonGetImage);
  getImageButton.setOnClickListener( new View.OnClickListener() {   
   @Override
   public void onClick(View v) {    
     Context context = v.getContext();
     Editable ed = imageUrl.getText();
     Drawable image = ImageOperations(context,ed.toString(),"image.jpg");
     ImageView imgView = new ImageView(context);
     imgView = (ImageView)findViewById(R.id.imageView);
     imgView.setImageDrawable(image);    
   }
  });
    }    
    private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
  try {
   URL imageUrl = new URL(url);
   InputStream is = (InputStream) imageUrl.getContent();
   Drawable d = Drawable.createFromStream(is, "src");
   return d;
  } catch (MalformedURLException e) {
   e.printStackTrace();
   return null;
  } catch (IOException e) {
   e.printStackTrace();
   return null;
  }
 }
}

13 comentarii:

Anonymous said...

I've come across this many times now while looking for a solution to this, but I'm curious—what is the significance of the Context and saveFile parameters in the ImageOperations method? I don't see a time when they are referenced within the method, or any time where a Context is even necessary.

Anonymous said...

Why not just use a WebView?

Anonymous said...

Your code is not working for BITMAP images plz update it ...... I am finding difficulty in downloading a bitmap image ...

Android app development said...

Really this is one of the progressive post.Thanks for collective this type of Information relate to Android.This is one of the good mobile application.We are also providing some logic related to Android.
Android app developers

Anonymous said...

it is showing run time error

Anonymous said...

i'm getting error as "unfortunately webimage(my app name) has stopped..

flore said...

im get error "unfortunately" application has stopped , can anyone help me? :'(

WallyHale said...

Those getting errors, check the formatting of the main.xml, capitalisation of the tags is incorrect .. fixed it for me.
ie change to EditText, Image, ButtonView

Probably goes without saying, remove the comment tags from the XML declaration in the first line too ;)

rize wordpress said...

Excellent information and thanks for you are tutorials.

Android Apps Development

Preston Tyson said...

Information which you provided on web image is excellent and useful. Thanks for this. Keep doing such work.

mehak lakhani said...

Description - We are the best web design company in Karachi, Pakistan offers the best web development services, SEO, SMO, android application development, LOGO Design services etc in cheap rates.

Hairs Seo said...

Thanks for providing great information.Its very easy to understand and helpful to me Apps Development Android

Areeba Memon said...

Custom Android App Development is said to have an instructing position in the Smartphone market as Android is a mind rousing stage to make Smartphone applications.

Post a Comment