Showing posts with label image. Show all posts
Showing posts with label image. Show all posts

April 29, 2011

How to use AsyncTask to load images on Android

If you are looking for an example of Haw to load an image from web you can find it here. If you need that the images were loaded asynchronously, find below an full example.















Add into your AndroidManifest.xml this permission:



main.xml


 
 

 
  
  
 
 
  
  
 
 



main.java
package com.blog.usingasynctask;

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.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class main extends Activity {
 
 final String[] links = {
      "http://www.android.com/media/wallpaper/gif/android_logo.gif",
      "http://www.android.com/media/wallpaper/gif/cupcake_2009.gif",
      "http://www.android.com/media/wallpaper/gif/donut_2009.gif",
      "http://www.android.com/media/wallpaper/gif/eclair_2009.gif",
      "http://www.android.com/media/wallpaper/gif/androids.gif"          
    };
 TextView count; 
 int id;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        id = 0;
        count = (TextView) findViewById(R.id.textView2);  
        startDownload( id );
    }
    
    public void startDownload(int id){
     // calling the asynchronous task 
     new ImageLoadingTask().execute();
    };
    
    public void displayImage(Drawable image){
     
     ImageView iv = null;
   
     switch (id){
      case 0 : iv = (ImageView) findViewById(R.id.imageView1);
       break;
      case 1 : iv = (ImageView) findViewById(R.id.imageView2);
       break;
      case 2 : iv = (ImageView) findViewById(R.id.imageView3);
    break;
      case 3 : iv = (ImageView) findViewById(R.id.imageView4);
    break;
      case 4 : iv = (ImageView) findViewById(R.id.imageView5);
    break;
     
     }
     
     iv.setImageDrawable(image);
     id++;
     if (id < 5) new ImageLoadingTask().execute();
    };

 public class ImageLoadingTask extends AsyncTask{

  @Override
  protected void onPreExecute(){   
   count.setText( count.getText() + " ... in progress image "+ id+1 );
  }
  
  @Override
  protected Drawable doInBackground(Void... params) {   
   //write here code that can take a long time   
   Drawable image = ImageOperations(getApplicationContext() ,links[id], id +".gif");    
   //publishProgress calls onProgressUpdate to update somthing on UI
   publishProgress();   
   
   return image;
  }
  
  @Override
  // Call this method when doInBackground is working to update the progress 
  protected void onProgressUpdate(Void... values) {  
   int number = id+1;
   count.setText("Loaded " + number + "/5");
  }
  
  @Override 
  protected void onPostExecute(Drawable result) {        
   // update the UI with loaded information
   displayImage(result);    
  }
  
  // Additional method to load images from web
   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;
    }
   }
 }
}

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;
  }
 }
}