Android Bitmap Create decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight)

Here you can find the source of decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight)

Description

decode Sampled Bitmap From File

Declaration

public static Bitmap decodeSampledBitmapFromFile(String filePath,
            int reqWidth, int reqHeight) 

Method Source Code

//package com.java2s;

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

import android.text.TextUtils;

public class Main {
    public static Bitmap decodeSampledBitmapFromFile(String filePath,
            int reqWidth, int reqHeight) {
        if (TextUtils.isEmpty(filePath)) {
            return null;
        }/*from ww  w  . j av a 2 s.  c  o m*/

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPurgeable = true;
        options.inInputShareable = true;
        BitmapFactory.decodeFile(filePath, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        options.inJustDecodeBounds = false;

        try {
            return BitmapFactory.decodeFile(filePath, options);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if ((reqWidth > 0 && reqHeight > 0)
                && (height > reqHeight || width > reqWidth)) {
            if (width > height) {
                inSampleSize = Math.round((float) height
                        / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return 0 == inSampleSize ? 1 : inSampleSize;
    }
}

Related

  1. GetUrlDrawable(String url)
  2. create2DBitmap(Bitmap bitmap)
  3. decodeFromBytes(byte[] bs, int reqWidth, int reqHeight)
  4. decodeFromResource(Resources res, int resId)
  5. decodeFromResource(Resources res, int resId, int reqWidth, int reqHeight)
  6. decodeSampledBitmapFromResource(Resources res, int id, int reqWidth, int reqHeight)
  7. decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
  8. getBitmap(Context context, InputStream inputStream)
  9. getImage(String absPath)