Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.IOException;
import java.util.HashMap;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    private static HashMap<String, Bitmap> sCacheMap = new HashMap<String, Bitmap>();

    /** load the Bitmap from assets or sdcard_dir
     * @param context
     * @param outsideFileName
     * @param op
     * @param useSdcard
     * @param sdcard_assetdir_path
     * @return
     */
    public static Bitmap loadBitmap(Context context, String outsideFileName, BitmapFactory.Options op,
            boolean useSdcard, String sdcard_assetdir_path) {
        AssetManager am = context.getAssets();

        Bitmap retval = null;
        synchronized (sCacheMap) {
            if (sCacheMap.get(outsideFileName) != null) {
                retval = sCacheMap.get(outsideFileName);
                return retval;
            }
        }

        try {
            retval = BitmapFactory.decodeStream(am.open(outsideFileName), null, op);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (useSdcard) {
            Bitmap _tmp = BitmapFactory.decodeFile(sdcard_assetdir_path + outsideFileName, op);
            if (_tmp != null) {
                retval = _tmp;
            }
        }

        if (retval != null) {
            synchronized (sCacheMap) {
                sCacheMap.put(outsideFileName, retval);
            }

        }

        return retval;
    }
}