package com.xlythe.view.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.widget.ImageView;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Loads images from disc into an {@link ImageView}.
 */
public class Image {
    private static Image sImage;

    private final Context mContext;
    private final Map<ImageView, Task> mImageTasks = new HashMap<>();

    public static Image with(Context context) {
        if (sImage == null) {
            sImage = new Image(context);
        }
        return sImage;
    }

    private Image(Context context) {
        mContext = context.getApplicationContext();
    }

    /**
     * @param file The image file.
     * @return A handler to the task that will load the image. Call {@link Loader#into(ImageView)}
     *         to finalize the task.
     */
    public Loader load(File file) {
        return new Loader(file);
    }

    /**
     * Releases an ImageView from any pending transactions.
     */
    public static void clear(ImageView imageView) {
        if (sImage != null) {
            Task task = sImage.mImageTasks.remove(imageView);
            if (task != null) {
                task.cancel(true);
            }
        }
    }

    /**
     * Loads an image file into an {@link ImageView}.
     */
    public class Loader {
        private final File mFile;

        private Loader(File file) {
            mFile = file;
        }

        /**
         * @param imageView The ImageView that should display the final result.
         */
        public void into(ImageView imageView) {
            Task task = new Task(imageView);
            mImageTasks.put(imageView, task);
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mFile);
        }
    }

    private static class Task extends AsyncTask<File, Void, Bitmap> {
        private final ImageView mImageView;

        Task(ImageView imageView) {
            mImageView = imageView;
        }

        @Override
        protected void onPreExecute() {
            mImageView.setImageDrawable(null);
        }

        @Override
        protected Bitmap doInBackground(File... params) {
            try {
                File file = params[0];
                Exif exif = new Exif(file);

                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

                Matrix matrix = new Matrix();
                matrix.postRotate(exif.getRotation());
                if (exif.isFlippedHorizontally()) {
                    matrix.postScale(-1, 1);
                }
                if (exif.isFlippedVertically()) {
                    matrix.postScale(1, -1);
                }

                return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            } catch (IOException e) {
                cancel(true);
                return null;
            }
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            mImageView.setImageBitmap(result);
        }
    }
}

@@ -180,20 +180,69 @@
 oid.
-os.AsyncTask
+net.Uri;%0Aimport android.os.AsyncTask;%0Aimport android.util.Log
 ;%0Aim
@@ -453,16 +453,64 @@
 Image %7B%0A
+    private static final String TAG = %22Image%22;%0A%0A
     priv
@@ -1152,32 +1152,318 @@
 r(file);%0A    %7D%0A%0A
+    /**%0A     * @param uri A uri to a local file.%0A     * @return A handler to the task that will load the image. Call %7B@link Loader#into(ImageView)%7D to%0A     *         finalize the task.%0A     */%0A    public Loader load(Uri uri) %7B%0A        return new Loader(new File(uri.getPath()));%0A    %7D%0A%0A
     /**%0A     * R
@@ -2876,16 +2876,140 @@
 Path());
+%0A                if (bitmap == null) %7B%0A                    throw new IOException(%22Unable to decode file%22);%0A                %7D
 %0A%0A      
@@ -3439,24 +3439,24 @@
 rix, true);%0A
-
             
@@ -3477,24 +3477,93 @@
 eption e) %7B%0A
+                Log.e(TAG, %22Failed to decode file %22 + params%5B0%5D, e);%0A
             

Update Image.java