com.binkery.app.filemanager.fragments.ThumbnailLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.binkery.app.filemanager.fragments.ThumbnailLoader.java

Source

/**
 * The MIT License (MIT)
 *
 * Copyright 2014 Binkery Huang <binkery@gmail.com>
 *
 * Oct 24, 2014
 */
package com.binkery.app.filemanager.fragments;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.binkery.app.filemanager.utils.Logs;

import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

public class ThumbnailLoader {

    private LruCache<String, Bitmap> mCache = null;
    private Context mContext;

    private ThumbnailLoader() {

    }

    public static ThumbnailLoader mLoader = null;

    public static ThumbnailLoader getInstance(Context context) {
        if (mLoader == null) {
            mLoader = new ThumbnailLoader();
            mLoader.mContext = context;
            final int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
                    .getMemoryClass();

            // Use 1/8th of the available memory for this memory cache.
            final int cacheSize = 1024 * 1024 * memClass / 8;
            mLoader.mCache = new LruCache<String, Bitmap>(cacheSize);
            mLoader.mTaskList = new ArrayList<ThumbnailLoader.TaskItem>();
        }
        return mLoader;
    }

    public Bitmap loadMicroThumbnail(String path, ImageView iv) {
        Bitmap bitmap = getBitmapFromMemCache(path);
        if (bitmap != null) {
            return bitmap;
        }
        for (TaskItem item : mTaskList) {
            if (item.path.equals(path)) {
                return null;
            }
        }

        TaskItem item = new TaskItem();
        item.path = path;
        item.imageview = iv;
        synchronized (mLock) {
            mTaskList.add(item);
        }
        if (!mIsLoadTaskRunning) {
            new ImageLoadTask().execute("");
        }
        return null;
    }

    private List<TaskItem> mTaskList = null;

    private class TaskItem {
        public String path;
        public ImageView imageview;
    }

    private void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        if (getBitmapFromMemCache(key) == null) {
            mCache.put(key, bitmap);
        }
    }

    private static final String TAG = ThumbnailLoader.class.getSimpleName();

    private Bitmap getBitmapFromMemCache(String key) {
        return mCache.get(key);
    }

    private Object mLock = new Object();
    private boolean mIsLoadTaskRunning = false;

    private class ImageLoadTask extends AsyncTask<String, TaskItem, String> {

        private int count = 0;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mIsLoadTaskRunning = true;
            Logs.i(TAG, "start loading . task list size = " + mTaskList.size());
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            mIsLoadTaskRunning = false;
            Logs.i(TAG, "load completed , task done " + count);
        }

        @Override
        protected void onProgressUpdate(TaskItem... values) {
            super.onProgressUpdate(values);
            count++;
            String current = (String) values[0].imageview.getTag();
            if (current != null && current.equals(values[0].path)) {
                values[0].imageview.setImageBitmap(getBitmapFromMemCache(values[0].path));
            } else {
                Logs.i(TAG, "thumbnail pass");
            }
        }

        @Override
        protected String doInBackground(String... params) {
            while (true) {
                TaskItem item = null;
                synchronized (mLock) {
                    try {
                        item = mTaskList.remove(0);
                    } catch (Exception e) {

                    }
                }
                if (item == null) {
                    break;
                }
                Bitmap bitmap = ThumbnailLoaderHelper.getImageThumbnail(new File(item.path), mContext);//(item.path);
                if (bitmap != null) {
                    addBitmapToMemoryCache(item.path, bitmap);
                    publishProgress(item);
                }

            }
            return null;
        }

        private Bitmap getImageThumbnail(String imagePath) {
            //         Bitmap bitmap = null;
            //         BitmapFactory.Options options = new BitmapFactory.Options();
            //         options.inJustDecodeBounds = true;
            //         bitmap = BitmapFactory.decodeFile(imagePath, options);
            //         options.inJustDecodeBounds = false; 
            //         int h = options.outHeight;
            //         int w = options.outWidth;
            //         int beWidth = w / 96;
            //         int beHeight = h / 96;
            //         int be = 1;
            //         if (beWidth < beHeight) {
            //            be = beWidth;
            //         } else {
            //            be = beHeight;
            //         }
            //         if (be <= 0) {
            //            be = 1;
            //         }
            //         Logs.i(TAG, "be = " + power(be,1));
            //         options.inSampleSize = be;
            //         long time = System.currentTimeMillis();
            //         bitmap = BitmapFactory.decodeFile(imagePath, options);
            //         Logs.i(TAG, "time  = " + (System.currentTimeMillis() - time));
            //         DocumentsContract.
            //         MediaStore.Images.Media.q
            //         ThumbnailUtils.e
            File file = new File(imagePath);
            return FileUtils.getThumbnail(mContext, file);
            //         bitmap = ThumbnailUtils.extractThumbnail(bitmap, 96, 96);
            //         return bitmap;
        }

    }

    private int power(int number, int p) {
        if (number <= 1) {
            return 1;
        }
        if (number < p * 2) {
            return p;
        }
        return power(number, p * 2);
    }

}