Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;

public class Main {
    public static Bitmap loadThumb(Context context, String thumbName) {
        if (thumbName == null || thumbName.length() <= 0)
            return null;
        File pictureFile = getOutputMediaFile(context, thumbName);
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath(), options);
            return bitmap;
        } catch (Exception e) {
            return null;
        }
    }

    private static File getOutputMediaFile(Context context, String thumbName) {
        File file = new File(context.getCacheDir() + "/thumbnails/" + thumbName + ".png");
        if (!file.exists()) {
            File mediaStorageDir = new File(context.getCacheDir() + "/thumbnails");

            // Create the storage directory if it does not exist
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    return null;
                }
            }
            // Create a media file name
            String mImageName = thumbName + ".png";
            File mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
            return mediaFile;
        } else {
            return file;
        }
    }
}