Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;

import java.util.Date;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v4.util.LruCache;

public class Main {
    private static String sdRootPath = Environment.getExternalStorageDirectory().getPath();
    private static String appRootPath = null;
    private static LruCache<String, Bitmap> bitmapCache;
    private final static String FOLDER_NAME = "/md4uImages";

    public static Bitmap getBitmap(String fileName) {
        Bitmap bitmap = getBitmapFromCache(fileName);
        if (bitmap != null) {
            return bitmap;
        }

        // modify the file attribute
        String filePath = getStorageDirectory() + File.separator + fileName;
        File file = new File(filePath);
        long time = new Date().getTime();
        file.setLastModified(time);
        return BitmapFactory.decodeFile(filePath);
    }

    private static Bitmap getBitmapFromCache(String key) {
        checkBitmapCache();
        return bitmapCache.get(key);
    }

    private static String getStorageDirectory() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? sdRootPath + FOLDER_NAME
                : appRootPath + FOLDER_NAME;
    }

    private static void checkBitmapCache() {
        if (bitmapCache == null) {
            bitmapCache = new LruCache<String, Bitmap>((int) Runtime.getRuntime().maxMemory() / 8) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight();
                }
            };
        }
    }
}