/*
 * Copyright (c) 2012 Bohemian Wrappsody AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.wrapp.android.webimage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

public class FileLoaderThread extends TaskQueueThread {
  private static FileLoaderThread staticInstance;

  public FileLoaderThread() {
    super("FileLoader");
    setPriority(Thread.NORM_PRIORITY);
  }

  public static FileLoaderThread getInstance() {
    if(staticInstance == null) {
      staticInstance = new FileLoaderThread();
    }
    return staticInstance;
  }

  @Override
  protected Bitmap processRequest(ImageRequest request) {
    Bitmap bitmap = null;

    File cacheFile = new File(ImageCache.getCacheDirectory(request.context), request.imageKey);
    if(cacheFile.exists()) {
      try {
        Date now = new Date();
        long fileAgeInMs = now.getTime() - cacheFile.lastModified();
        if(fileAgeInMs > ImageCache.CACHE_RECHECK_AGE_IN_MS) {
          CheckTimestampThread.getInstance().addTask(request);
        }

        LogWrapper.logMessage("Loading image " + request.imageUrl + " from file cache");
        // TODO: decodeFileDescriptor might be faster, see http://stackoverflow.com/a/7116158/14302
        bitmap = BitmapFactory.decodeStream(new FileInputStream(cacheFile), null, request.loadOptions);
        if(bitmap == null) {
          throw new Exception("Could not create bitmap from image " + request.imageUrl.toString());
        }
      }
      catch(Exception e) {
        LogWrapper.logException(e);
      }
    }

    return bitmap;
  }
}

@@ -1280,16 +1280,44 @@
 Stream;%0A
+import java.io.IOException;%0A
 import j
@@ -1802,16 +1802,56 @@
  null;%0A%0A
+    FileInputStream inputStream = null;%0A
     File
@@ -2317,99 +2317,53 @@
     
-// TODO: decodeFileDescriptor might be faster, see http://stackoverflow.com/a/7116158/14302
+inputStream = new FileInputStream(cacheFile);
 %0A   
@@ -2400,44 +2400,41 @@
 code
-Stream(new FileInputStream(cacheFile
+FileDescriptor(inputStream.getFD(
 ), n
@@ -2674,24 +2674,238 @@
 e);%0A      %7D%0A
+      finally %7B%0A        if(inputStream != null) %7B%0A          try %7B%0A            inputStream.close();%0A          %7D%0A          catch(IOException e) %7B%0A            LogWrapper.logException(e);%0A          %7D%0A        %7D%0A      %7D%0A
     %7D%0A%0A    r

Use BitmapFactory.decodeFileDescriptor to read img