Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;

import java.io.File;

public class Main {
    private static String IMAGE_TYPT_BMP = "bmp";
    private static String IMAGE_TYPT_JPG = "jpg";
    private static String IMAGE_TYPT_JPEG = "jpeg";
    private static String IMAGE_TYPT_PNG = "png";
    private static String IMAGE_TYPT_GIF = "gif";

    private static Bitmap getThumbnail(String filePath, int targetW, int targetH) {
        Bitmap bitmap = null;

        File file = new File(filePath);
        String fileName = file.getName();
        if (fileName.lastIndexOf(".") < 0) {
            return bitmap;
        }

        String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);

        if (fileExtension.equalsIgnoreCase(IMAGE_TYPT_BMP) || fileExtension.equalsIgnoreCase(IMAGE_TYPT_JPG)
                || fileExtension.equalsIgnoreCase(IMAGE_TYPT_JPEG) || fileExtension.equalsIgnoreCase(IMAGE_TYPT_PNG)
                || fileExtension.equalsIgnoreCase(IMAGE_TYPT_GIF)) {

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, options);
            int bitmapRealWidth = options.outWidth;
            int bitmapRealHeight = options.outHeight;
            options.outWidth = targetW;
            if (bitmapRealWidth > 0) {
                options.outHeight = bitmapRealHeight * targetW / bitmapRealWidth;
            }

            options.inJustDecodeBounds = false;

            if (targetW > 0) {
                options.inSampleSize = bitmapRealWidth / targetW;
            }

            options.inPurgeable = true;
            options.inInputShareable = true;
            options.inPreferredConfig = Config.ARGB_4444;

            bitmap = BitmapFactory.decodeFile(filePath, options);
        }

        return bitmap;
    }
}