| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 
 | package com.dvp.android.gallery;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.ArrayList;
 
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
 
/**
 * @author Mickaël Le Trocquer
 */
public class DVPGallery extends Activity {
 
	//Adresse où se trouve l'ensemble des images gif (numérotées de 1 à 21).
	private final static String SERVER_IM = "http://www.e-kom.fr/p_public/test12789go/uploads/";
 
	// GUI
	private Gallery gallery;
	private ImageView imgView;
 
	//Data
	private ArrayList<URL> mListImages;
	private Drawable mNoImage;
 
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
 
		//Récupération d'une image par défaut à afficher en cas d'erreur ou de liste vide
		mNoImage = this.getResources().getDrawable(R.drawable.ic_launcher);
 
		//Construction des URL pour les images
//		mListImages = buildListImages();
	        new AT_buildListImages().execute("");
 
	        Runtime.getRuntime().gc();
	}
 
 
	/**
         * Permet de construire la liste des urls pour les images
         * @return
         */
//	private ArrayList<URL> buildListImages() {
	public class AT_buildListImages extends AsyncTask<String, Void, Void> {
 
	        ProgressDialog Dialog = new ProgressDialog(DVPGallery.this);
 
	        protected void onPreExecute() {
//    		super.onPreExecute();
 
	            Dialog.setMessage("Loading images...");
        	    Dialog.show();
	        }
 
            @Override
       	    protected Void doInBackground(String... param) {
    		int nbTotalImage = 2;
//		int nbTotalImage = files.length;
    		ArrayList<URL> listFic = new ArrayList<URL>();
    		for(int i = 1; i <= nbTotalImage; i++) {
    			try {
    				listFic.add(new URL(SERVER_IM + "/" + i + ".gif"));
    			} catch (MalformedURLException e) {
    				Log.e("DVP Gallery", "Erreur format URL : " + SERVER_IM + "/" + i + ".gif");
    				e.printStackTrace();
    			}
    		}
 
	        Runtime.getRuntime().gc();
    		mListImages = listFic;
    		return null;
        	}
 
            protected void onPostExecute(Void unused) {
//            	super.onPostExecute(unused);
 
            	connect_1st();
 
                // Close progress dialog
                Dialog.dismiss();
            }
    	}
 
	public Void connect_1st() {
 
		//Récupération du composant affichant l'image en grand
		imgView = (ImageView)findViewById(R.id.imageview);
 
		//On lui met une image par défaut (la premiere de la liste ou à défaut l'image d'erreur)
		if (mListImages.size() <= 0) {
			imgView.setImageDrawable(mNoImage); 
		} else {
			setImage(imgView, mListImages.get(0));
		}
		//Récupération du composant affichant la liste des vignettes
		gallery = (Gallery) findViewById(R.id.gallery);
		//On lui donne notre adapter qui s'occupéra de l'alimenter en vignette
		gallery.setAdapter(new AddImgAdp(this));
		//Espacement entre les vignette
		gallery.setSpacing(10);
 
		//Lors d'un clic sur une des vignettes, on affiche l'image correspondante en grand
		gallery.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position, long id) {			
				setImage(imgView, mListImages.get(position));
			}
		});
 
            Runtime.getRuntime().gc();
	    return null;
	}
 
 
	/**
         * Notre adapter qui gère la liste des vignettes
         * @author Mickaël Le Trocquer
         */
	public class AddImgAdp extends BaseAdapter {
		int GalItemBg;
		private Context cont;
 
		public AddImgAdp(Context c) {
			cont = c;
			TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
			GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
			typArray.recycle();
		}
 
		public int getCount() {
			return mListImages.size();
		}
 
		public Object getItem(int position) {
			return position;
		}
 
		public long getItemId(int position) {
			return position;
		}
 
		public View getView(final int position, View convertView, ViewGroup parent) {
			ImageView imgView = null;
			//Récyclage du composant
			if (convertView == null) {
				imgView = new ImageView(cont);
			} else {
				imgView = (ImageView)convertView;
			}
			//On affecte notre image à la vignette
			if (mListImages.size() <= 0) {
				imgView.setImageDrawable(mNoImage); 
			} else {
				setImage(imgView, mListImages.get(position));
			}
			//On défini la taille de l'image
			imgView.setLayoutParams(new Gallery.LayoutParams(150, 150));
			imgView.setScaleType(ImageView.ScaleType.FIT_XY);
			//On fixe un arrière plan plus sympa
			imgView.setBackgroundResource(GalItemBg);
 
			return imgView;
		}
	}
 
 
	/**
         * Méthode permettant de télécharger une image depuis une URL et de l'affecter à un composant de type ImageView
         * @param aView
         * @param aURL
         */
	public void setImage(ImageView aView, URL aURL) {
		try {
			URLConnection conn = aURL.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
 
			// Bufferisation pour le téléchargement 
			BufferedInputStream bis = new BufferedInputStream(is);
 
	                Runtime.getRuntime().gc();
			// Création de l'image depuis le flux des données entrant
			Bitmap bm = BitmapFactory.decodeStream(bis);
			int dstWidth = 150, dstHeight = 150;
			bm.createScaledBitmap(bm, dstWidth, dstHeight, true);
			bis.close();
			is.close();
 
			// Fixe l'image sur le composant ImageView
			aView.setImageBitmap(bm);
//			if (bm != null) bm.recycle();
 
		} catch (IOException e) {
			aView.setImageDrawable(mNoImage);
			Log.e("DVP Gallery", "Erreur téléchargement image URL : " + aURL.toString());
			e.printStackTrace();
		} 
	}
} | 
Partager