AsyncTask

Absolument nécessaire lorsque l’on travaille avec des pages web.

Exemple de récupération d’une image :

Appel de la tâche Asynchrone :

 

 String[] myParams={  
 FicheAnnonce.getImgAnnonce(),  
 FicheAnnonce.getNumAnnonce(),  
 FicheAnnonce.getUrlAnnonce(),  
 FicheAnnonce.getPriAnnonce(),  
 FicheAnnonce.getTitAnnonce()  
 };  
 new LoadImage().execute(myParams);  

Tâche Asynchrone :

  /*  
   X – The type of the input variables value you want to set to the background process. This can be an array of objects.  
   Y – The type of the objects you are going to enter in the onProgressUpdate method.  
   Z – The type of the result from the operations you have done in the background process.  
   */  
   private class LoadImage extends AsyncTask<String, String, Bitmap> {  
     String sLNAnn, SLUAnn, sLPAnn, sLTann;  
     @Override  
     protected void onPreExecute() {  
       super.onPreExecute();        
       pDialog = new ProgressDialog(MainActivity.this);  
       pDialog.setMessage("Loading Image ....");  
       pDialog.show();        
     }  
     protected Bitmap doInBackground(String... args) {  
       Bitmap bitTmp=null;  
       try {  
         bitTmp = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());  
       } catch (Exception e) {  
         e.printStackTrace();  
       }  
       sLNAnn=args[1];  
       SLUAnn=args[2];  
       sLPAnn=args[3];  
       sLTann=args[4];  
       Log.d("AsyncNum",sLNAnn);  
       return bitTmp;  
     }  
     protected void onPostExecute(Bitmap image) {  
       byte[] imageAnn = null;  
       AnnonceDbHandler dbHandler = new AnnonceDbHandler(getApplicationContext(), null, null, 1);  
       if (image != null) {  
         //////  
         Resources res = getResources();  
         int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);  
         int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);  
         image = Bitmap.createScaledBitmap(image, width, height, false);  
         //////  
         int size = image.getRowBytes() * image.getHeight();  
         ByteBuffer byteBuffer = ByteBuffer.allocate(size);  
         image.copyPixelsToBuffer(byteBuffer);  
         imageAnn = byteBuffer.array();  
       } else {  
         Log.d("ERROR", "Image Does Not exist or Network Error");  
         //-- Décodage et stockage du symbol "NO PHOTO"  
         Resources res = getResources();  
         ByteArrayOutputStream stream;  
         Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.nophoto, null);  
         image = (Bitmap) ((BitmapDrawable) drawable).getBitmap();  
         int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);  
         int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);  
         image = Bitmap.createScaledBitmap(image, width, height, false);  
         stream = new ByteArrayOutputStream();  
         image.compress(Bitmap.CompressFormat.JPEG, 100, stream);  
         //TabImg.add(stream.toByteArray());  
         imageAnn = stream.toByteArray();  
         Log.d("ERREUR", "Impossible de charger l'image");  
       }  
       if (!dbHandler.ExistAnnonce(sLNAnn, SLUAnn, imageAnn))  
         addNotification(sLNAnn, SLUAnn, sMessage + sLPAnn, sLTann, false, false, image);  
     }  
   }  

Références :

http://stackoverflow.com/questions/3075009/android-how-can-i-pass-parameters-to-asynctasks-onpreexecute

Articles récents
Commentaires récents
fatima dans Bienvenue !
AdminDroid dans Bienvenue !
fatima dans Bienvenue !
Archives
Catégories