Android: Carga asíncrona de imágenes por http
Para cargar imagenes remotas en el layout de forma asíncrona, sin bloquear la actividad.
Podéis usar esta clase:
package com.tu.package;
import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
@Override
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
@Override
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Y para usarla…
new DownloadImageTask((ImageView) findViewById(R.id.idDelImageView)).execute("http://www.emm-gfx.net/wp-content/uploads/2012/09/thumb-mosh.png");