Android Bitmap Resize readBitmapAutoSize(String filePath, int outWidth, int outHeight)

Here you can find the source of readBitmapAutoSize(String filePath, int outWidth, int outHeight)

Description

read Bitmap Auto Size

Declaration

public static Bitmap readBitmapAutoSize(String filePath, int outWidth,
            int outHeight) 

Method Source Code

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

import java.io.FileInputStream;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap readBitmapAutoSize(String filePath, int outWidth,
            int outHeight) {
        FileInputStream fs = null;
        BufferedInputStream bs = null;
        try {//from w  ww  . j  a  v a2s . co m
            fs = new FileInputStream(filePath);
            bs = new BufferedInputStream(fs);
            BitmapFactory.Options options = setBitmapOption(filePath,
                    outWidth, outHeight);
            return BitmapFactory.decodeStream(bs, null, options);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bs.close();
                fs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static BitmapFactory.Options setBitmapOption(String file,
            int width, int height) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file, opt);

        int outWidth = opt.outWidth;
        int outHeight = opt.outHeight;
        opt.inDither = false;
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inSampleSize = 1;
        if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0) {
            int sampleSize = (outWidth / width + outHeight / height) / 2;
            opt.inSampleSize = sampleSize;
        }

        opt.inJustDecodeBounds = false;
        return opt;
    }
}

Related

  1. calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
  2. createFitCenterBitmap(Bitmap srcBitmap, int dstWidth, int dstHeight, boolean tryRecycleSource)
  3. createFitXYBitmap(Bitmap srcBitmap, int dstWidth, int dstHeight, boolean tryRecycleSource)
  4. getSampledBitmap(String filePath, int reqWidth, int reqHeight)
  5. manageBitmapRotatio(int photoW, int photoH, Bitmap bitMap, int rotation)
  6. resize(Bitmap bitmap, int newWidth, int newHeight)
  7. resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle)
  8. resizeAndCropCenter(final Bitmap bitmap, final int size)
  9. resizeBitmap(Context context, int resId, int w, int h)