load Bitmap by max side length - Android Graphics

Android examples for Graphics:Bitmap File

Description

load Bitmap by max side length

Demo Code


import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import java.io.*;

public class Main{
    public static Bitmap loadBitmap(String path, int maxSideLen) {
        if (null == path) {
            return null;
        }//from www .ja v a  2s  .  co  m
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        options.inJustDecodeBounds = false;
        options.inSampleSize = Math.max(options.outWidth / maxSideLen,
                options.outHeight / maxSideLen);
        if (options.inSampleSize < 1) {
            options.inSampleSize = 1;
        }
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        try {
            Bitmap bitmap = BitmapFactory.decodeFile(path, options);
            if (bitmap != bitmap) {
                bitmap.recycle();
            }
            return bitmap;
        } catch (OutOfMemoryError e) {
            Debug.debug("higo", e.toString());
        }
        return null;
    }
    public static Bitmap loadBitmap(String path) {
        if (null == path) {
            return null;
        }
        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inSampleSize = 1;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        try {
            Bitmap bitmap = BitmapFactory.decodeFile(path, options);
            return bitmap;
        } catch (OutOfMemoryError e) {
            Debug.debug("higo", e.toString());
        }
        return null;
    }
}

Related Tutorials