Android Open Source - Aviary-Android-SDK Download Image Async Task






From Project

Back to project page Aviary-Android-SDK.

License

The source code is released under:

AVIARY API TERMS OF USE Full Legal Agreement The following terms and conditions and the terms and conditions at http://www.aviary.com/terms (collectively, the ?Terms??) govern your use of any and ...

If you think the Android project Aviary-Android-SDK 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.aviary.android.feather.async_tasks;
/*from   ww  w .jav  a 2s .c o m*/
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;

import com.aviary.android.feather.common.log.LoggerFactory;
import com.aviary.android.feather.common.log.LoggerFactory.Logger;
import com.aviary.android.feather.common.log.LoggerFactory.LoggerType;
import com.aviary.android.feather.common.utils.SystemUtils;
import com.aviary.android.feather.common.utils.os.AviaryAsyncTask;
import com.aviary.android.feather.library.Constants;
import com.aviary.android.feather.library.utils.DecodeUtils;
import com.aviary.android.feather.library.utils.ImageSizes;

// TODO: Auto-generated Javadoc
/**
 * Load an Image bitmap asynchronous.
 * 
 * @author alessandro
 */
public class DownloadImageAsyncTask extends AviaryAsyncTask<Context, Void, Bitmap> {

  /**
   * The listener interface for receiving onImageDownload events. The class that is
   * interested in processing a onImageDownload
   * event implements this interface, and the object created with that class is
   * registered with a component using the component's
   * <code>addOnImageDownloadListener<code> method. When
   * the onImageDownload event occurs, that object's appropriate
   * method is invoked.
   * 
   * @see OnImageDownloadEvent
   */
  public static interface OnImageDownloadListener {

    /**
     * On download start.
     */
    void onDownloadStart();

    /**
     * On download complete.
     * 
     * @param result
     *            the result
     */
    void onDownloadComplete( Bitmap result, ImageSizes sizes );

    /**
     * On download error.
     * 
     * @param error
     *            the error
     */
    void onDownloadError( String error );
  };

  private OnImageDownloadListener mListener;
  private Uri mUri;
  private String error;
  private ImageSizes mImageSize;
  private int mMaxSize;

  /**
   * Instantiates a new download image async task.
   * 
   * @param uri
   *            the uri
   */
  public DownloadImageAsyncTask ( Uri uri, int maxSize ) {
    super();
    mUri = uri;
    mMaxSize = maxSize;
  }

  /**
   * Sets the on load listener.
   * 
   * @param listener
   *            the new on load listener
   */
  public void setOnLoadListener( OnImageDownloadListener listener ) {
    mListener = listener;
  }

  @Override
  protected void PreExecute() {
    Log.i( "DownloadImageAsyncTask", "PreExecute: current thread #" + Thread.currentThread().getId() );
    if ( mListener != null ) mListener.onDownloadStart();
    mImageSize = new ImageSizes();
  }

  @Override
  protected Bitmap doInBackground( Context... params ) {
    Log.i( "DownloadImageAsyncTask", "doInBackground: current thread #" + Thread.currentThread().getId() );

    Context context = params[0];

    int max_size = -1;

    if ( mMaxSize > 0 ) {
      max_size = mMaxSize;
    }

    if ( max_size <= 0 ) {
      max_size = getManagedMaxImageSize( context );
    }

    try {
      return DecodeUtils.decode( context, mUri, max_size, max_size, mImageSize );
    } catch ( Exception e ) {
      Logger logger = LoggerFactory.getLogger( "DownloadImageTask", LoggerType.ConsoleLoggerType );
      logger.error( "error", e.getMessage() );
      error = e.getMessage();
    }
    return null;
  }

  @Override
  protected void PostExecute( Bitmap result ) {
    Log.i( "DownloadImageAsyncTask", "PostExecute: current thread #" + Thread.currentThread().getId() );
    if ( mListener != null ) {
      if ( result != null ) {
        mListener.onDownloadComplete( result, mImageSize );
      } else {
        mListener.onDownloadError( error );
      }
    }

    if ( mImageSize.getOriginalSize() == null ) {
      mImageSize.setOriginalSize( mImageSize.getNewSize() );
    }

    mListener = null;
    mUri = null;
    error = null;
  }

  /**
   * Return the maximum image size allowed for this device. Be careful if you want to
   * modify the return value because it's easy to
   * throw an {@link OutOfMemoryError} in android expecially when dealing with
   * {@link Bitmap}.<br />
   * Part of the application available memory has been already taken by the host
   * application.
   * 
   * @return the managed max image size
   */
  protected int getManagedMaxImageSize( Context context ) {

    final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    final int screen_size = Math.max( metrics.widthPixels, metrics.heightPixels );
    double applicationMemory = SystemUtils.getApplicationTotalMemory();

    if ( applicationMemory >= Constants.APP_MEMORY_MEDIUM ) {
      return Math.min( screen_size, 1280 );
    } else if ( applicationMemory >= Constants.APP_MEMORY_SMALL ) {
      return Math.min( screen_size, 900 );
    } else {
      return Math.min( screen_size, 700 );
    }
  }
}




Java Source Code List

com.aviary.android.feather.AlertActivity.java
com.aviary.android.feather.AviaryMainController.java
com.aviary.android.feather.FeatherActivity.java
com.aviary.android.feather.async_tasks.AsyncImageManager.java
com.aviary.android.feather.async_tasks.DownloadImageAsyncTask.java
com.aviary.android.feather.async_tasks.ExifTask.java
com.aviary.android.feather.effects.AbstractContentPanel.java
com.aviary.android.feather.effects.AbstractOptionPanel.java
com.aviary.android.feather.effects.AbstractPanelLoaderService.java
com.aviary.android.feather.effects.AbstractPanel.java
com.aviary.android.feather.effects.AdjustEffectPanel.java
com.aviary.android.feather.effects.BordersPanel.java
com.aviary.android.feather.effects.ColorSplashPanel.java
com.aviary.android.feather.effects.CropPanel.java
com.aviary.android.feather.effects.DelayedSpotDrawPanel.java
com.aviary.android.feather.effects.DrawingPanel.java
com.aviary.android.feather.effects.EffectsPanel.java
com.aviary.android.feather.effects.EnhanceEffectPanel.java
com.aviary.android.feather.effects.MemePanel.java
com.aviary.android.feather.effects.NativeEffectRangePanel.java
com.aviary.android.feather.effects.SimpleStatusMachine.java
com.aviary.android.feather.effects.SliderEffectPanel.java
com.aviary.android.feather.effects.StickersPanel.java
com.aviary.android.feather.effects.TextPanel.java
com.aviary.android.feather.effects.TiltShiftPanel.java
com.aviary.android.feather.graphics.CdsPreviewTransformer.java
com.aviary.android.feather.graphics.GalleryBottomIndicatorDrawable.java
com.aviary.android.feather.graphics.GalleryTopIndicatorDrawable.java
com.aviary.android.feather.graphics.GlowBitmapDrawable.java
com.aviary.android.feather.graphics.GlowDrawable.java
com.aviary.android.feather.graphics.PluginDividerDrawable.java
com.aviary.android.feather.graphics.PreviewFillColorDrawable.java
com.aviary.android.feather.graphics.PreviewSpotDrawable.java
com.aviary.android.feather.graphics.RepeatableHorizontalDrawable.java
com.aviary.android.feather.opengl.AviaryGLSurfaceView.java
com.aviary.android.feather.utils.PackIconCallable.java
com.aviary.android.feather.utils.SimpleBitmapCache.java
com.aviary.android.feather.utils.ThreadUtils.java
com.aviary.android.feather.utils.TypefaceUtils.java
com.aviary.android.feather.utils.UIUtils.java
com.aviary.android.feather.widget.AdjustImageView.java
com.aviary.android.feather.widget.AviaryAbsSpinner.java
com.aviary.android.feather.widget.AviaryAdapterView.java
com.aviary.android.feather.widget.AviaryBadgeToolLayout.java
com.aviary.android.feather.widget.AviaryBottomBarViewFlipper.java
com.aviary.android.feather.widget.AviaryButton.java
com.aviary.android.feather.widget.AviaryEdgeEffect.java
com.aviary.android.feather.widget.AviaryGalleryTopIndicatorView.java
com.aviary.android.feather.widget.AviaryGallery.java
com.aviary.android.feather.widget.AviaryHighlightImageButton.java
com.aviary.android.feather.widget.AviaryImageRestoreSwitcher.java
com.aviary.android.feather.widget.AviaryImageSwitcher.java
com.aviary.android.feather.widget.AviaryNavBarViewFlipper.java
com.aviary.android.feather.widget.AviarySeekBar.java
com.aviary.android.feather.widget.AviaryTextView.java
com.aviary.android.feather.widget.AviaryToast.java
com.aviary.android.feather.widget.AviaryToggleButton.java
com.aviary.android.feather.widget.AviaryWheel.java
com.aviary.android.feather.widget.AviaryWorkspaceIndicator.java
com.aviary.android.feather.widget.AviaryWorkspace.java
com.aviary.android.feather.widget.CellLayout.java
com.aviary.android.feather.widget.CropImageView.java
com.aviary.android.feather.widget.DrawableHighlightView.java
com.aviary.android.feather.widget.EffectThumbLayout.java
com.aviary.android.feather.widget.HighlightView.java
com.aviary.android.feather.widget.IAPBuyButton.java
com.aviary.android.feather.widget.IAPDialogDetail.java
com.aviary.android.feather.widget.IAPDialogList.java
com.aviary.android.feather.widget.IAPDialogMain.java
com.aviary.android.feather.widget.ImageViewDrawableOverlay.java
com.aviary.android.feather.widget.ImageViewSpotDraw.java
com.aviary.android.feather.widget.ImageViewTiltiShiftTouch.java
com.aviary.android.feather.widget.ImageViewTouchAndDraw.java
com.aviary.android.feather.widget.PointCloud.java
com.aviary.android.feather.widget.ScrollerRunnable.java
com.aviary.android.feather.widget.VibrationHelper.java
com.aviary.android.feather.widget.VibrationWidget.java