Android Open Source - lightbox-android-webservices Bitmap Loader






From Project

Back to project page lightbox-android-webservices.

License

The source code is released under:

Apache License

If you think the Android project lightbox-android-webservices 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) 2012 Lightbox/*www  . java 2  s . c  o  m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lightbox.android.bitmap;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

import com.lightbox.android.cache.BitmapCache;
import com.lightbox.android.network.HttpHelper;
import com.lightbox.android.network.HttpHelper.HttpMethod;
import com.lightbox.android.utils.debug.DebugLog;

/** 
 * This class allows you to load a bitmap, synchronously or in the background, taking care for you of memory caching,
 * disk caching, and background task.
 * @see BitmapSource
 * @author Fabien Devos & Nilesh Patel
 */
public class BitmapLoader {
  /** Used to tag logs */
  //@SuppressWarnings("unused")
  private static final String TAG = "BitmapLoader";

  private BitmapLoaderTask mBitmapLoaderTask;
  
  //----------------------------------------------
  // Bitmap Loading API
  
  public enum Volatility {
    DEFAULT,
    DO_NOT_CACHE,
  }
  
  public static Bitmap load(BitmapSource bitmapSource, BitmapSource.Type type, Config config, Volatility volatility) throws IOException {
    // Try to get bitmap from memory cache
    Bitmap bitmap = BitmapCache.getInstance().getFromMemory(bitmapSource.getAbsoluteFileName(type));
    
    // Else load from file on disk, or network
    if (bitmap == null) {
      bitmap = loadFromDiskOrNetwork(bitmapSource, type, config, volatility);
    }
    
    return bitmap;
  }

  public void loadAsync(BitmapSource bitmapSource, BitmapSource.Type type, Config config, Volatility volatility, BitmapLoaderListener listener) {
    // Try to get bitmap from memory cache on main thread
    Bitmap bitmap = BitmapCache.getInstance().getFromMemory(bitmapSource.getAbsoluteFileName(type));
        
    if (bitmap == null) {
      DebugLog.d(TAG, "Unable to get Bitmap from memory cache, fetching from disk or network. Title: %s - Type: %s - Absolute file name: %s", bitmapSource.getTitle(), type, bitmapSource.getAbsoluteFileName(type));

      mBitmapLoaderTask = new BitmapLoaderTask(bitmapSource, type, config, volatility, listener);
      mBitmapLoaderTask.execute();
    } else {
      listener.onLoaded(bitmap, type, true); 
    }    
  }
  
  public void cancel() {
    if (mBitmapLoaderTask != null) {
      mBitmapLoaderTask.cancel();
    }
  }
  
  //----------------------------------------------
  // Loading
  
  /*package*/ static Bitmap loadFromDiskOrNetwork(BitmapSource bitmapSource, BitmapSource.Type type, Config config, Volatility volatility) throws IOException {
    Bitmap bitmap = null;
    
    try {
      bitmap = loadFromDiskOrNetworkWithOutOfMemoryRisk(bitmapSource, type, config, volatility);
      
    } catch (OutOfMemoryError e) {
      DebugLog.d(TAG, "OutOfMemoryError! Trying to reduce the size of the bitmap memory cache.");
      BitmapCache.getInstance().decreaseMemoryCacheSize();
    }
    
    return bitmap;
  }
  
  /*package*/ static Bitmap loadFromDiskOrNetworkWithOutOfMemoryRisk(BitmapSource bitmapSource, BitmapSource.Type type, Config config, Volatility volatility) throws IOException {
    // Try to load it from disk
    Bitmap bitmap = BitmapCache.getInstance().getFromDisk(bitmapSource.getAbsoluteFileName(type), config).getData();
    
    // Else try to get it from network
    if (bitmap == null) {
      //Log.e(TAG, "CALLING SERVER file: %s" + bitmapSource.getAbsoluteFileName(type));
      DebugLog.d(TAG, "Unable to get Bitmap from disk cache, fetching from network. Title: %s - Type: %s - Absolute file name: %s", bitmapSource.getTitle(), type, bitmapSource.getAbsoluteFileName(type));

      bitmap = getBitmapFromNetwork(bitmapSource, type, config);
    }
    
    // Put in memory cache
    if (volatility == Volatility.DEFAULT) {
      BitmapCache.getInstance().putInMemory(bitmapSource.getAbsoluteFileName(type), bitmap);
    }
    
    return bitmap;
  }
    
  //----------------------------------------------
  // Bitmap from Network

  private static Bitmap getBitmapFromNetwork(BitmapSource bitmapSource, BitmapSource.Type type, Config config) {
    Bitmap bitmap = null;
    URI uri = bitmapSource.getUri(type);
    if (uri != null) {
      FileOutputStream fos = null;
      try {
        File file = new File(bitmapSource.getAbsoluteFileName(type));
        
        // Ensure that the directory exist
        file.getParentFile().mkdirs();
        
        HttpResponse httpResponse = HttpHelper.getInstance().call(HttpMethod.GET, uri, null);
        
        fos = new FileOutputStream(file);
        
        IOUtils.copy(httpResponse.getEntity().getContent(), fos);
        
        bitmap = BitmapCache.getInstance().getFromDisk(bitmapSource.getAbsoluteFileName(type), config).getData();
      } catch (IOException e) {
        DebugLog.d("", e.getMessage());
        // Return null
      } finally {
        IOUtils.closeQuietly(fos);
      }
    }
    return bitmap;
  }
  
}




Java Source Code List

com.lightbox.android.bitmap.BitmapFileCleanerTask.java
com.lightbox.android.bitmap.BitmapLoaderListener.java
com.lightbox.android.bitmap.BitmapLoaderTask.java
com.lightbox.android.bitmap.BitmapLoader.java
com.lightbox.android.bitmap.BitmapSize.java
com.lightbox.android.bitmap.BitmapSource.java
com.lightbox.android.bitmap.BitmapUtils.java
com.lightbox.android.cache.AbstractCache.java
com.lightbox.android.cache.ApiCache.java
com.lightbox.android.cache.BitmapCache.java
com.lightbox.android.cache.Cache.java
com.lightbox.android.data.ClearAndSaveBatchTask.java
com.lightbox.android.data.Data.java
com.lightbox.android.data.DatabaseCleanerTask.java
com.lightbox.android.data.DatabaseHelper.java
com.lightbox.android.data.DeleteBatchTask.java
com.lightbox.android.data.SaveBatchTask.java
com.lightbox.android.io.RandomAccessFileOutputStream.java
com.lightbox.android.lifecycle.LifeCycleListener.java
com.lightbox.android.lifecycle.ManagedLifeCycleActivity.java
com.lightbox.android.location.LocationHelper.java
com.lightbox.android.location.LocationListener.java
com.lightbox.android.network.HttpHelper.java
com.lightbox.android.network.NetworkUtils.java
com.lightbox.android.operations.AbstractOperation.java
com.lightbox.android.operations.CachedOperation.java
com.lightbox.android.operations.DeleteOperation.java
com.lightbox.android.operations.FailureOperation.java
com.lightbox.android.operations.ModificationNetworkOnlyOperation.java
com.lightbox.android.operations.NetworkOnlyOperation.java
com.lightbox.android.operations.OperationListener.java
com.lightbox.android.operations.OperationTask.java
com.lightbox.android.operations.Operation.java
com.lightbox.android.operations.Retrievable.java
com.lightbox.android.operations.RetrieveOperation.java
com.lightbox.android.operations.SaveOperation.java
com.lightbox.android.operations.Updatable.java
com.lightbox.android.tasks.BackgroundTaskWeak.java
com.lightbox.android.tasks.BackgroundTask.java
com.lightbox.android.tasks.DefaultExecutor.java
com.lightbox.android.utils.AndroidUtils.java
com.lightbox.android.utils.Base64.java
com.lightbox.android.utils.IntentUtils.java
com.lightbox.android.utils.MediaUtils.java
com.lightbox.android.utils.ResUtils.java
com.lightbox.android.utils.debug.DebugLifeCycleListener.java
com.lightbox.android.utils.debug.DebugLog.java
com.lightbox.android.views.RemoteImageView.java
com.lightbox.android.views.RemoteThumbImageView.java
com.lightbox.android.webservices.processors.GenerationException.java
com.lightbox.android.webservices.processors.JacksonProcessor.java
com.lightbox.android.webservices.processors.ParsingException.java
com.lightbox.android.webservices.processors.Processor.java
com.lightbox.android.webservices.requests.ApiRequestFactory.java
com.lightbox.android.webservices.requests.ApiRequestListener.java
com.lightbox.android.webservices.requests.ApiRequestTask.java
com.lightbox.android.webservices.requests.ApiRequest.java
com.lightbox.android.webservices.requests.ApiRequests.java
com.lightbox.android.webservices.requests.JacksonApiRequestFactory.java
com.lightbox.android.webservices.responses.ApiException.java
com.lightbox.android.webservices.responses.ApiResponse.java
com.lightbox.tweetsnearby.TweetsNearbyApplication.java
com.lightbox.tweetsnearby.activities.MainActivity.java
com.lightbox.tweetsnearby.activities.PickPlaceActivity.java
com.lightbox.tweetsnearby.model.Tweet.java
com.lightbox.tweetsnearby.model.Venue.java
com.lightbox.tweetsnearby.responses.foursquare.FoursquareApiResponse.java
com.lightbox.tweetsnearby.responses.foursquare.VenueListResponse.java
com.lightbox.tweetsnearby.responses.twitter.TweetListResponse.java
com.lightbox.tweetsnearby.responses.twitter.TwitterApiResponse.java