Android Open Source - Alkomer Image Download Async Task






From Project

Back to project page Alkomer.

License

The source code is released under:

MIT License

If you think the Android project Alkomer 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 cz.jmx.tomik.alkomer.android.threads;
//from  w  ww . j  a v a  2 s.c om
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Alkomer - Server App
 * --------------------
 * Downloads Image and Displays it (ASyncTask)
 *
 * @copyright   Copyright (c) 2011 Tomas Vitek
 * @author      Tomas Vitek ~ http://tomik.jmx.cz

 * @package     Alkomer
 * @version     1.0
 */
public class ImageDownloadAsyncTask extends AsyncTask<String, Void, Bitmap> {

  protected Activity screen;
  protected ImageView placer;
  
  public ImageDownloadAsyncTask(Activity screen, ImageView placer) {
    this.placer = placer;
    this.screen = screen;
  }
  
  protected void onPostExecute(Bitmap image) {
    placer.clearAnimation();     
    placer.setImageBitmap(image);
    
    if (image == null) {
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(screen);
        if (!prefs.getBoolean("chartErrorShown", false)) {      
          Toast.makeText(screen, "P?i na??t?n? graf? nastala chyba! Zkontrolujte, zda m?te funk?n? p?ipojen? k internetu. \n\nPokud chyba p?etrv?v?, zkuste to pros?m pozd?ji.", Toast.LENGTH_SHORT).show();
          
          Editor editor = prefs.edit();
          editor.putBoolean("chartErrorShown", true);
          editor.commit();            
        }      
    }
    }

  @Override
  protected Bitmap doInBackground(String... arg0) {
    
    String url = arg0[0];
    
    Log.d("Downloading IMAGE", url);
    
    URL myFileUrl = null; 
    try {
      myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
      Log.e("ImageDownload Error", e.getMessage());
    }
    try {
      HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
      conn.setDoInput(true);
      conn.connect();
      InputStream is = conn.getInputStream();
  
      return BitmapFactory.decodeStream(is);
    } catch (IOException e) {
      Log.e("ImageDownload Error", e.getMessage());
    }
    
    return null;    
    
  }
}




Java Source Code List

cz.jmx.tomik.alkomer.android.BaseActivity.java
cz.jmx.tomik.alkomer.android.DashboardActivity.java
cz.jmx.tomik.alkomer.android.DrinkListActivity.java
cz.jmx.tomik.alkomer.android.ForgottenPasswordActivity.java
cz.jmx.tomik.alkomer.android.GlassListActivity.java
cz.jmx.tomik.alkomer.android.LoginActivity.java
cz.jmx.tomik.alkomer.android.Main.java
cz.jmx.tomik.alkomer.android.RegistrationActivity.java
cz.jmx.tomik.alkomer.android.SettingsActivity.java
cz.jmx.tomik.alkomer.android.StatisticsActivity.java
cz.jmx.tomik.alkomer.android.StatisticsFlowAdapter.java
cz.jmx.tomik.alkomer.android.database.DataHelper.java
cz.jmx.tomik.alkomer.android.drinks.DrinkAdapter.java
cz.jmx.tomik.alkomer.android.drinks.Drink.java
cz.jmx.tomik.alkomer.android.drinks.Drinks.java
cz.jmx.tomik.alkomer.android.drinks.GlassAdapter.java
cz.jmx.tomik.alkomer.android.drinks.Glass.java
cz.jmx.tomik.alkomer.android.models.Gender.java
cz.jmx.tomik.alkomer.android.models.User.java
cz.jmx.tomik.alkomer.android.threads.DeleteDrinkThread.java
cz.jmx.tomik.alkomer.android.threads.DeleteDrinksThread.java
cz.jmx.tomik.alkomer.android.threads.DrinkThread.java
cz.jmx.tomik.alkomer.android.threads.ForgottenPasswordAsyncTask.java
cz.jmx.tomik.alkomer.android.threads.ImageDownloadAsyncTask.java
cz.jmx.tomik.alkomer.android.threads.LoginAsyncTask.java
cz.jmx.tomik.alkomer.android.threads.RegistrationAsyncTask.java
cz.jmx.tomik.alkomer.android.threads.UpdateSettingsThread.java
cz.jmx.tomik.alkomer.android.tools.Calculator.java
cz.jmx.tomik.alkomer.android.tools.DataTime.java
cz.jmx.tomik.alkomer.android.tools.EmailValidator.java
cz.jmx.tomik.alkomer.android.tools.IntEditTextPreference.java