Help with AsyncTask and web images

All your problems with Audio, Video and Images.

Help with AsyncTask and web images

Postby niknjen » Tue Apr 12, 2011 7:41 am

I am having problems with AsyncTask.

I would like to wrap the following code for loading images from the web in an AsyncTask so The UI dosen't bog down while the image is loaded.

Code: Select all
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;
}
}
}



Thanks
niknjen
Freshman
Freshman
 
Posts: 8
Joined: Tue Feb 15, 2011 8:18 am

Top

Re: Help with AsyncTask and web images

Postby imbroken » Fri Apr 15, 2011 8:19 am

You just need to call LoadImageFromWebOperations(String url) in your asyncTask in doInBackround();
imbroken
Senior Developer
Senior Developer
 
Posts: 131
Joined: Thu Jan 06, 2011 6:57 pm
Location: Germnay

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 15 guests