Example usage for android.graphics BitmapFactory decodeFile

List of usage examples for android.graphics BitmapFactory decodeFile

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeFile.

Prototype

public static Bitmap decodeFile(String pathName, Options opts) 

Source Link

Document

Decode a file path into a bitmap.

Usage

From source file:Main.java

public static final Bitmap getBitmap(String fileName) {
    Bitmap bitmap = null;//from  www. j  a va2  s. c  om
    try {
        Options options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(fileName, options);
        options.inSampleSize = Math.max(1, (int) Math
                .ceil(Math.max((double) options.outWidth / 1024f, (double) options.outHeight / 1024f)));
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(fileName, options);
    } catch (OutOfMemoryError error) {
        error.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap decodeFile(File f, int maxSize) {
    // decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;/*from w  w  w .  ja va2s. c  om*/
    BitmapFactory.decodeFile(f.getAbsolutePath(), o);
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 < maxSize || height_tmp / 2 < maxSize)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale++;
    }

    // decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeFile(f.getAbsolutePath(), o2);
}

From source file:Main.java

public static Bitmap decodeBitmap(String imagePath, int targetWidth, int targetHeight) {
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    /* Figure out which way needs to be reduced less */
    int scaleFactor = 1;
    if ((targetWidth > 0) || (targetHeight > 0)) {
        scaleFactor = Math.min(photoW / targetWidth, photoH / targetHeight);
    }//w ww  . j  ava 2 s. c  o m

    /* Set bitmap options to scale the image decode target */
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    /* Decode the JPEG file into a Bitmap */
    return BitmapFactory.decodeFile(imagePath, bmOptions);

}

From source file:Main.java

public static Bitmap decodeBitmap(File file, int reqHeight, int reqWidth) {
    // Get image size of file
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  w  w w .  j a  va2s  . c om
    BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    final int height = options.outHeight;
    final int width = options.outWidth;

    // Calculate sample size
    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        options.inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    // Decode bitmap
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inPurgeable = true;
    options.inDither = false;
    return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
}

From source file:Main.java

public static Bitmap resizeImage(String path, int targetH, int targetW) {

    Options bmOptions = new Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;/*from w ww .  ja  v  a  2 s  . c om*/

    return BitmapFactory.decodeFile(path, bmOptions);

}

From source file:Main.java

public static Bitmap getScaledBitmap(String picturePath, int width, int height) {
    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(picturePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor;

    if (width > 0 && height > 0) {
        scaleFactor = Math.min(photoW / width, photoH / height);
    } else {/*w ww. j ava2  s  .  c o m*/
        scaleFactor = 1;
    }

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    return BitmapFactory.decodeFile(picturePath, bmOptions);
}

From source file:Main.java

public static Bitmap readBitmapFromPath(Activity context, String filePath) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from  ww  w  .j av a2  s  .c  o  m
    BitmapFactory.decodeFile(filePath, options);
    int outWidth = options.outWidth;
    int outHeight = options.outHeight;
    options.inJustDecodeBounds = false;
    int be = calculateInSampleSize(context, outWidth, outHeight);
    options.inSampleSize = be;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    try {
        return BitmapFactory.decodeFile(filePath, options);
    } catch (OutOfMemoryError e) {
        System.gc();
        try {
            options.inSampleSize = be + 1;
            return BitmapFactory.decodeFile(filePath, options);

        } catch (OutOfMemoryError e2) {
            return null;
        }
    }
}

From source file:Main.java

static public int getImageSize(String albumArtPath) {
    if (albumArtPath != null) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;//w ww  .  j  a  v a 2  s .  c o m
        Bitmap bmTmp = BitmapFactory.decodeFile(albumArtPath, opts);
        if (bmTmp != null)
            bmTmp.recycle();
        return Math.max(opts.outWidth, opts.outWidth);
    }
    return 0;
}

From source file:Main.java

public static Bitmap getBitmapBySize(String path, int width, int height) {
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inJustDecodeBounds = true;//  w ww. ja v  a2s. c  om
    BitmapFactory.decodeFile(path, option);
    option.inSampleSize = computeSampleSize(option, -1, width * height);

    option.inJustDecodeBounds = false;
    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeFile(path, option);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return bitmap;
}

From source file:Main.java

public static int[] decodeSampleSize(String path, float reqWidth, float reqHeight) {
    Options opts = new Options();
    opts.inJustDecodeBounds = true;// w w  w  .  j  a  va2s. c o  m
    opts.inPreferredConfig = Config.RGB_565;
    BitmapFactory.decodeFile(path, opts);
    opts.inSampleSize = calculateInSampleSize(opts, reqWidth, reqHeight);
    BitmapFactory.decodeFile(path, opts);
    return new int[] { opts.outWidth, opts.outHeight };
}