Android Open Source - AndroIUT Bitmap Downloader Task






From Project

Back to project page AndroIUT.

License

The source code is released under:

GNU General Public License

If you think the Android project AndroIUT listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.iutdijon.androiut2.util.loaders;
/*  w w  w.jav a  2 s  . c om*/
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Log;

import com.iutdijon.androiut2.R;
import com.iutdijon.androiut2.ade.activities.OnImageDownloadedCallback;
import com.iutdijon.androiut2.util.view.ImageZoomView;

/**
 * Classe permettant de t?l?charger une image dans un processus ? part
 * @author Morgan Funtowicz
 *
 */
public class BitmapDownloaderTask extends AsyncTask<HashMap<String, String>, Void, Bitmap> {

    private final WeakReference<ImageZoomView> imageViewReference;
    private static final String iut_webapi_url = "http://iutdijon.u-bourgogne.fr/intra/iq/AndroIUT/webapi.php";
    private final Context context;
    private ProgressDialog progressBar;
    private OnImageDownloadedCallback callback;
    
    public BitmapDownloaderTask(Context c, ImageZoomView mZoomView) {
        imageViewReference = new WeakReference<ImageZoomView>(mZoomView);
        context = c;
    }

    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      progressBar = ProgressDialog.show(context, "", context.getText(R.string.ftp_command_running));
    }
    @Override
    // Actual download method, run in the task thread
    protected Bitmap doInBackground(HashMap<String, String>... params) {
         // params comes from the execute() call: params[0] is the url.
        return downloadBitmap(params[0]);
    }

    @Override
    // Once the image is downloaded, associates it to the imageView
    protected void onPostExecute(Bitmap bitmap) {
//        if (isCancelled()) {
//            bitmap = null;
//        }
      if(progressBar != null){
        if(progressBar.isShowing()){
          progressBar.dismiss();
          progressBar = null;
        }
      }
        if (imageViewReference != null) {
          ImageZoomView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImage(bitmap);
            }
        }
        if(callback != null){
          callback.onDownloaded();
        }
    }
    /**
     * D?marre le t?l?chargement de l'image suivant les param?tres ?ventuellement pass? ? l'URL
     * Les param?tres sont envoy?s via la m?thode POST avec le protocol HTTP
     * @param parameters La map contenant tous les param?tres ? ajouter ? la requ?te
     * @return Un Bitmap ou null si l'image est invalide
     */
    static Bitmap downloadBitmap(HashMap<String, String> parameters) {
        final AndroidHttpClient client = AndroIUTHTTPConnection.getHTTPClient();
        final HttpPost postRequest = new HttpPost();
        
        try {
          Set<String> parameters_keys = parameters.keySet();

        HttpPost post_query = new HttpPost(iut_webapi_url);

        ArrayList<NameValuePair> post_params = new ArrayList<NameValuePair>();
        post_params.add(new BasicNameValuePair("function", "getPlanning"));
        for(String key : parameters_keys){
          post_params.add(new BasicNameValuePair(key, parameters.get(key)));
        }

        post_query.setEntity(new UrlEncodedFormEntity(post_params));
        AndroidHttpClient.modifyRequestToAcceptGzipResponse(post_query);
        
        
        HttpResponse response = client.execute(post_query);

            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) { 
                Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + iut_webapi_url); 
                return null;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = AndroidHttpClient.getUngzippedContent(entity); 
                    final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                } finally {
                    if (inputStream != null) {
                        inputStream.close();  
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            // Could provide a more explicit error message for IOException or IllegalStateException
            postRequest.abort();
            Log.w("ImageDownloader", e.toString());
        } finally {
            if (client != null) {
                client.close();
            }
        }
        return null;
    }
    
    /**
     * Permet de d?finir un callback lorsque l'image est t?l?charg?e
     * @param callback
     */
    public void setOnImageDownloadedCallback(OnImageDownloadedCallback callback){
      this.callback = callback;
    }
}




Java Source Code List

.ADEResourcesDataTree.java
com.iutdijon.androiut2.ade.activities.ADEActivity.java
com.iutdijon.androiut2.ade.activities.ADEListResourcesChooser.java
com.iutdijon.androiut2.ade.activities.OnImageDownloadedCallback.java
com.iutdijon.androiut2.ade.service.ADEICalendarDownloader.java
com.iutdijon.androiut2.ftp.activities.FTPActivity.java
com.iutdijon.androiut2.ftp.adapters.FTPFileListAdapter.java
com.iutdijon.androiut2.ftp.data.FTPCommandResult.java
com.iutdijon.androiut2.ftp.data.FTPCommandWrapper.java
com.iutdijon.androiut2.ftp.data.FTPCommand.java
com.iutdijon.androiut2.ftp.services.FTPDownloadMonitor.java
com.iutdijon.androiut2.ftp.services.FTPServiceCall.java
com.iutdijon.androiut2.ftp.services.ProgressDownload.java
com.iutdijon.androiut2.global.AndroIUTApplication.java
com.iutdijon.androiut2.iut.activities.HomeActivity.java
com.iutdijon.androiut2.iut.activities.LoginActivity.java
com.iutdijon.androiut2.iut.data.Credential.java
com.iutdijon.androiut2.iut.data.UserFactory.java
com.iutdijon.androiut2.iut.data.account.StudentAccount.java
com.iutdijon.androiut2.iut.data.account.TeacherAccount.java
com.iutdijon.androiut2.iut.data.account.UserAccount.java
com.iutdijon.androiut2.iut.services.IUTLoginServiceCall.java
com.iutdijon.androiut2.mail.activities.MailActivity.java
com.iutdijon.androiut2.mail.activities.MailReaderActivity.java
com.iutdijon.androiut2.mail.adapters.MailDisplayAdapter.java
com.iutdijon.androiut2.mail.adapters.MailsListAdapter.java
com.iutdijon.androiut2.mail.services.EmailUtils.java
com.iutdijon.androiut2.mail.services.MailReader.java
com.iutdijon.androiut2.mail.services.MailService.java
com.iutdijon.androiut2.schooling.activities.SchoolingActivity.java
com.iutdijon.androiut2.schooling.activities.SchoolingTabActivity.java
com.iutdijon.androiut2.schooling.adapters.SectionedAdapter.java
com.iutdijon.androiut2.schooling.data.Absence.java
com.iutdijon.androiut2.schooling.data.Mark.java
com.iutdijon.androiut2.schooling.data.SchoolingData.java
com.iutdijon.androiut2.schooling.service.SchoolingDataServiceCall.java
com.iutdijon.androiut2.util.AndroIUTLogger.java
com.iutdijon.androiut2.util.IOUtils.java
com.iutdijon.androiut2.util.PreferencesManager.java
com.iutdijon.androiut2.util.UIUtil.java
com.iutdijon.androiut2.util.adapters.AbsencesAdapter.java
com.iutdijon.androiut2.util.adapters.BitmapAdapter.java
com.iutdijon.androiut2.util.adapters.CalendarAdapter.java
com.iutdijon.androiut2.util.adapters.IAdapter.java
com.iutdijon.androiut2.util.adapters.MarksAdapter.java
com.iutdijon.androiut2.util.adapters.UserAdapter.java
com.iutdijon.androiut2.util.adapters.XmlAdapter.java
com.iutdijon.androiut2.util.bridge.BridgeFinder.java
com.iutdijon.androiut2.util.loaders.AndroIUTHTTPConnection.java
com.iutdijon.androiut2.util.loaders.AsyncServiceCall.java
com.iutdijon.androiut2.util.loaders.BitmapDownloaderTask.java
com.iutdijon.androiut2.util.loaders.GetAsyncServiceCall.java
com.iutdijon.androiut2.util.loaders.ImageDownloader.java
com.iutdijon.androiut2.util.loaders.PostAsyncServiceCall.java
com.iutdijon.androiut2.util.observers.Observable.java
com.iutdijon.androiut2.util.observers.Observer.java
com.iutdijon.androiut2.util.security.SecureCenter.java
com.iutdijon.androiut2.util.view.ImageZoomView.java
com.iutdijon.androiut2.util.zoom.AspectQuotient.java
com.iutdijon.androiut2.util.zoom.DynamicZoomControl.java
com.iutdijon.androiut2.util.zoom.LongPressZoomListener.java
com.iutdijon.androiut2.util.zoom.PinchZoomListener.java
com.iutdijon.androiut2.util.zoom.SimpleZoomListener.java
com.iutdijon.androiut2.util.zoom.ZoomState.java
com.iutdijon.androiut2.util.zoom.util.Dynamics.java
com.iutdijon.androiut2.util.zoom.util.SpringDynamics.java