Hace poco buscando en GitHub.com encontré una librería que a más de uno ayudará en sus desarrollos. Se trata de una librería que se encarga de descargar imágenes, almacenarlas en caché y gestionar de donde se coge la imagen si se vuelve a pedir la misma url de forma totalmente transparente al programador. La librería en cuestión la podéis encontrar en el siguiente enlace https://github.com/novoda/ImageLoader.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
private static ImageManager imageManager;
@Override
public void onCreate() {
super.onCreate();
normalImageManagerSettings();
}
private void normalImageManagerSettings() {
imageManager = new ImageManager(this, new SettingsBuilder()
.withCacheManager(new LruBitmapCache(this))
.build(this));
}
/**
* There are different settings that you can use to customize
* the usage of the image loader for your application.
*/
@SuppressWarnings("unused")
private void verboseImageManagerSettings() {
SettingsBuilder settingsBuilder = new SettingsBuilder();
//You can force the urlConnection to disconnect after every call.
settingsBuilder.withDisconnectOnEveryCall(true);
//We have different types of cache, check cache package for more info
settingsBuilder.withCacheManager(new LruBitmapCache(this));
//You can set a specific read timeout
settingsBuilder.withReadTimeout(30000);
//You can set a specific connection timeout
settingsBuilder.withConnectionTimeout(30000);
//You can disable the multi-threading ability to download image
settingsBuilder.withAsyncTasks(false);
//You can set a specific directory for caching files on the sdcard
// settingsBuilder.withCacheDir(new File("/something"));
//Setting this to false means that file cache will use the url without the query part
//for the generation of the hashname
settingsBuilder.withEnableQueryInHashGeneration(false);
LoaderSettings loaderSettings = settingsBuilder.build(this);
imageManager = new ImageManager(this, loaderSettings);
}
/**
* Convenient method of access the imageLoader
*/
public static ImageManager getImageLoader() {
return imageManager;
}
<ImageView android:id="@+id/imgThumb" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initImageLoader();
}
private void initImageLoader(){
imageManager = AppGlobals.getImageLoader();
imageTagFactory = new ImageTagFactory(SIZE, SIZE, R.drawable.loading);
imageTagFactory.setErrorImageId(R.drawable.image_not_found);
imageTagFactory.setSaveThumbnail(true);
}
protected ImageManager imageManager;
protected ImageTagFactory imageTagFactory;
protected void loadImgView(ImageView imgView, String urlImage){
imgView.setTag(imageTagFactory.build(urlImage));
imageManager.getLoader().load(imgView);
}
ImageView imgThumb = (ImageView) findViewById(R.id.imgThumb);
loadImgView(imgThumb, "url de vuestra imagen");
<GridView
android:id="@+id/gridviewgaleria"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
String [] arrUrls;
private GridView gridviewGaleria;
public void setGridViewGaleria(){
arrUrls = new String[2];
arrUrls[0] ="Url 1";
arrUrls[1] ="Url 2";
gridviewGaleria = (GridView) findViewById(R.id.gridviewgaleria);
gridviewGaleria.setAdapter(new ImageAdapter(this));
gridviewGaleria.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
//Setear aquí lo que querais hacer al pinchar en una foto
}
});
}
public class ImageAdapter extends BaseAdapter{
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return arrURLs.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mContext);
image = new ImageView(mContext);
NombreDeMiActivity.this.loadImgView(image, arrURLs[position]); image.setLayoutParams(new GridView.LayoutParams(85, 85));
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setPadding(8, 8, 8, 8);
return image;
}
}