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 Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  w  ww .ja  v a2 s  .  c  om*/
    BitmapFactory.decodeFile(path, options);

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeFile(path, options);
    return bmp;
}

From source file:Main.java

public static Bitmap loadScreenScaledBitmap(String filename, Context context) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from  www  . j  av a 2 s .  c  o m
    BitmapFactory.decodeFile(filename, options);
    options.inSampleSize = calculateInSampleSize(options, context);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filename, options);
}

From source file:Main.java

public static boolean scaleImage(String origin_path, String result_path, int target_size) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  w  ww.j  a v  a 2  s . c  o  m
    BitmapFactory.decodeFile(origin_path, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    int scale = (int) (Math.sqrt(imageWidth * imageHeight * 4 / target_size)) + 1;
    Bitmap bmp = null;
    options.inJustDecodeBounds = false;
    options.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(origin_path, options);

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(result_path);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            bmp.recycle();
            if (out != null) {
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Bitmap decodeFileForRGB(String path) {
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inPreferredConfig = Config.RGB_565;
    opt.inSampleSize = 2;//from w  w  w  .j a  va2s  . c  o m
    opt.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, opt);
}

From source file:Main.java

public static Bitmap getCompressedBitmap(String filePath, int sampleSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = sampleSize;/*from  www  . ja v  a 2  s  .  co  m*/
    try {
        return BitmapFactory.decodeFile(filePath, options);
    } catch (OutOfMemoryError e) {
        Log.e("talktab.error", "OutOfMemoryError");
        options.inSampleSize = sampleSize * 2;
        return BitmapFactory.decodeFile(filePath, options);
    }
}

From source file:Main.java

public static Bitmap getBitmapThumbnail(String path, int width, int height) {
    Bitmap bitmap = null;/*ww w  .java 2 s.  co m*/
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    options.inJustDecodeBounds = false;
    int h = options.outHeight;
    int w = options.outWidth;
    int scaleWidth = w / width;
    int scaleHeight = h / height;
    int scale = 1;
    if (scaleWidth < scaleHeight) {
        scale = scaleWidth;
    } else {
        scale = scaleHeight;
    }
    if (scale <= 0) {
        scale = 1;
    }
    options.inSampleSize = scale;
    bitmap = BitmapFactory.decodeFile(path, options);
    return bitmap;
}

From source file:Main.java

public static Bitmap getScreenSizeBitmap(String filePath, int[] screenSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;//from   w w w. j  ava2 s .c  o m
    options.inSampleSize = getSampleSizeAdjustToScreen(filePath, screenSize);
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
    return bitmap;
}

From source file:Main.java

public static Bitmap loadEmptyBitmap(Context context, String file_path) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  w  w  w . jav  a 2  s .  c  o m
    BitmapFactory.decodeFile(file_path, options);
    Bitmap empty_bitmap = Bitmap.createBitmap(options.outWidth, options.outHeight, Config.ALPHA_8);
    return empty_bitmap;
}

From source file:Main.java

public static Bitmap decodeImage(String path) {
    try {//  w  w  w.ja v a  2 s  . com
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        options.inJustDecodeBounds = false;
        int be = (int) (options.outWidth / (float) ZEN_IMAGE_WIDTH);

        if (be <= 0) {
            be = 1;
        }

        options.inSampleSize = be;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        return bitmap;
    } catch (OutOfMemoryError error) {
        error.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Bitmap decodeSampledBitmap(String imagePath, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   ww w .  j ava 2s.c  o m*/
    BitmapFactory.decodeFile(imagePath, options);

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(imagePath, options);
}