Android Open Source - AsciiCamera Async Image Loader






From Project

Back to project page AsciiCamera.

License

The source code is released under:

Apache License

If you think the Android project AsciiCamera 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

// Copyright (C) 2014 Bruno Ramalhete
/*from  w w  w . jav  a  2s .c  o  m*/
package com.spectralsoftware.util;

import java.lang.ref.WeakReference;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.ImageView;

/** 
 * Class to handle assigning a bitmap to a view, when the bitmap may need to be loaded from
 * storage asynchronously. 
 * 
 * This code is based on the sample code at http://developer.android.com/training/displaying-bitmaps/index.html
 */
public class AsyncImageLoader {
    
    static class BitmapWorkerTask extends AsyncTask<Uri, Void, Bitmap> {
        WeakReference<ImageView> imageViewReference;
        Uri data = null;
        ScaledBitmapCache bitmapCache;
        int width;
        int height;

        public BitmapWorkerTask(ImageView imageView, ScaledBitmapCache bitmapCache, int width, int height) {
            // Use a WeakReference to ensure the ImageView can be garbage collected
            imageViewReference = new WeakReference<ImageView>(imageView);
            this.bitmapCache = bitmapCache;
            this.width = width;
            this.height = height;
        }

        // Decode image in background.
        @Override protected Bitmap doInBackground(Uri...args) {
            data = args[0];
            return bitmapCache.getScaledBitmap(args[0], width, height);
        }

        // Once complete, see if ImageView is still around and set bitmap.
        @Override protected void onPostExecute(Bitmap bitmap) {
            if (imageViewReference != null && bitmap != null) {
                final ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
        
        private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
            if (imageView != null) {
                final Drawable drawable = imageView.getDrawable();
                if (drawable instanceof AsyncDrawable) {
                    final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
                    return asyncDrawable.getBitmapWorkerTask();
                }
            }
            return null;
        }

        public static boolean cancelPotentialWork(Uri uri, ImageView imageView) {
            final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);

            if (bitmapWorkerTask != null) {
                final Uri taskUri = bitmapWorkerTask.data;
                if (!uri.equals(taskUri)) {
                    // Cancel previous task
                    bitmapWorkerTask.cancel(true);
                } 
                else {
                    // The same work is already in progress
                    return false;
                }
            }
            // No task associated with the ImageView, or an existing task was cancelled
            return true;
        }
    }

    static class AsyncDrawable extends BitmapDrawable {
        private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;

        @SuppressWarnings({ "rawtypes", "unchecked" })
    public AsyncDrawable(Resources res, AsyncTask<Uri, Void, Bitmap> bitmapWorkerTask) {
            super(res, (Bitmap)null);
            bitmapWorkerTaskReference = new WeakReference(bitmapWorkerTask);
        }

        public BitmapWorkerTask getBitmapWorkerTask() {
            return bitmapWorkerTaskReference.get();
        }
    }
    
    /**
     * Sets the bitmap of an ImageView, loading the bitmap using a background AsyncTask if needed.
     * If the image URI is found in-memory in the ScaledBitmapCache, assigns it directly to the
     * ImageView and returns. Otherwise, creates an AsyncTask in which the ScaledBitmapCache reads
     * the bitmap from secondary storage and assigns it to the ImageView when loaded.
     */
    public void loadImageIntoViewAsync(final ScaledBitmapCache bitmapCache, Uri imageUri, ImageView imageView, 
            final int width, final int height, Resources resources) {
        // check in-memory cache, if found no need for an AsyncTask
        Bitmap bitmap = bitmapCache.getInMemoryScaledBitmap(imageUri, width, height);
        if (bitmap!=null) {
            imageView.setImageBitmap(bitmap);
            return;
        }
        // read from storage with AsyncTask
        if (BitmapWorkerTask.cancelPotentialWork(imageUri, imageView)) {
            BitmapWorkerTask task = new BitmapWorkerTask(imageView, bitmapCache, width, height);
            AsyncDrawable asyncDrawable = new AsyncDrawable(resources, task);
            imageView.setImageDrawable(asyncDrawable);
            task.execute(imageUri);
        }
    }

}




Java Source Code List

com.spectralsoftware.asciicamera.AboutActivity.java
com.spectralsoftware.asciicamera.AsciiCamActivity.java
com.spectralsoftware.asciicamera.AsciiCamPreferences.java
com.spectralsoftware.asciicamera.AsciiConverter.java
com.spectralsoftware.asciicamera.AsciiImageWriter.java
com.spectralsoftware.asciicamera.AsciiRenderer.java
com.spectralsoftware.asciicamera.ImageDirectory.java
com.spectralsoftware.asciicamera.LibraryActivity.java
com.spectralsoftware.asciicamera.NewPictureReceiverLegacyBroadcast.java
com.spectralsoftware.asciicamera.NewPictureReceiver.java
com.spectralsoftware.asciicamera.OverlayView.java
com.spectralsoftware.asciicamera.ProcessImageOperation.java
com.spectralsoftware.asciicamera.ViewImageActivity.java
com.spectralsoftware.util.ARManager.java
com.spectralsoftware.util.AndroidUtils.java
com.spectralsoftware.util.AsyncImageLoader.java
com.spectralsoftware.util.CameraPreviewProcessingQueue.java
com.spectralsoftware.util.CameraUtils.java
com.spectralsoftware.util.ScaledBitmapCache.java
com.spectralsoftware.util.ShutterButton.java
com.spectralsoftware.util.SingleItemProcessingQueue.java