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 getUploadBitmap(String imagePath) {
    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;
    if (photoW < MAX_WIDTH && photoH < MAX_HEIGHT) {
        scaleFactor = 1;//from   w  ww .j  a v a 2 s.  c  om
    } else {
        scaleFactor = Math.max(photoW / MAX_WIDTH, photoH / MAX_HEIGHT);
    }
    Log.d(TAG, "scaleFactor:" + scaleFactor);
    /* Set bitmap options to scale the image decode target */
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    /* Decode the JPEG file into a Bitmap */
    return BitmapFactory.decodeFile(imagePath, bmOptions);

}

From source file:Main.java

/**
 * Get bitmap from internal image file.//from  w ww. j ava2s.com
 */
public static Bitmap getBitmapFromUri(Uri fileUri) {
    // bitmap factory
    BitmapFactory.Options options = new BitmapFactory.Options();

    // downsizing photoImage as it throws OutOfMemory Exception for larger
    // images
    options.inSampleSize = 8;
    options.inMutable = true;

    return BitmapFactory.decodeFile(fileUri.getPath(), options);
}

From source file:Main.java

/**
 * Determines if the file contains a valid image.
 *
 * @param pathName A file path./*from www .j  a v  a2 s  .c o m*/
 * @return {@code true} if the stream contains a valid image; {@code false} if not.
 */
public static boolean hasImageContent(String pathName) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);
    return options.outMimeType != null;
}

From source file:Main.java

public static Bitmap createScaledRotatedBitmapFromFile(String filePath, int reqWidth, int reqHeight,
        int orientation) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from   w  ww  . j a  v a 2s  . c om
    BitmapFactory.decodeFile(filePath, options);

    Bitmap bitmap;
    int rotationAngle = 0, outHeight = 0, outWidth = 0;
    if (reqWidth >= reqHeight) {
        if (options.outWidth >= options.outHeight) {
            if (orientation != 1) {
                rotationAngle = 180;
            } else {

            }
            bitmap = decodeSampledBitmapFromFile(filePath, reqWidth, reqHeight);
        } else {
            bitmap = decodeSampledBitmapFromFile(filePath, reqHeight, reqWidth);
        }
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
    } else {
        if (options.outWidth > options.outHeight) {
            if (orientation != 1) {
                rotationAngle = 90;
            } else {
                rotationAngle = 270;
            }
            bitmap = decodeSampledBitmapFromFile(filePath, reqHeight, reqWidth);
        } else {
            bitmap = decodeSampledBitmapFromFile(filePath, reqWidth, reqHeight);
        }
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
    }

    if (rotationAngle == 0) {
        return bitmap;
    }

    Matrix matrix = new Matrix();
    matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, outHeight, outWidth, matrix, true);
    if (bitmap != rotateBitmap) {
        bitmap.recycle();
    }

    return rotateBitmap;
}

From source file:Main.java

public static Bitmap getBitmap(String imagePath, int inSampleSize) {
    if (imagePath == null) {
        return null;
    }//from ww w.j  a va  2  s.co m
    if (inSampleSize == 0) {
        inSampleSize = 1;
    }

    Bitmap bmp = null;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
    opts.inSampleSize = inSampleSize;
    bmp = BitmapFactory.decodeFile(imagePath, opts);

    return bmp;
}

From source file:Main.java

public static Bitmap getFitSampleBitmap(String file_path, int width, int height) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*  www . jav a 2 s .c  o m*/
    BitmapFactory.decodeFile(file_path, options);
    options.inSampleSize = getFitInSampleSize(width, height, options);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(file_path, options);
}

From source file:Main.java

public static Bitmap makeBitmap(String filePath, int maxNumOfPixels) {
    try {// ww  w .  jav a2  s  .c  om
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        if (options.mCancel || options.outWidth == -1 || options.outHeight == -1) {
            return null;
        }
        options.inSampleSize = computeSampleSize(options, -1, maxNumOfPixels);
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap b = BitmapFactory.decodeFile(filePath, options);
        b = formatBitmap(b, Math.min(options.outHeight, options.outWidth));
        return b;
    } catch (OutOfMemoryError ex) {
        Log.e(TAG, "Got oom exception ", ex);
        return null;
    }
}

From source file:Main.java

public static Bitmap getRoundedBitmap(String path) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;//from  ww  w .  j  av a  2  s .  c  om

    return getRoundedCornerBitmap(BitmapFactory.decodeFile(path, options));
}

From source file:Main.java

public static Bitmap scaleBitmapToFitInView(View view, String imagePath) {
    // Get the dimensions of the View
    int targetW = view.getWidth();
    int targetH = view.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;//www  .j  a  va 2s .com

    Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
    return bitmap;
}

From source file:Main.java

public static Bitmap decodeFile(String fPath) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;//from   w w  w.j  a  v  a 2 s .c om
    opts.inDither = false; // Disable Dithering mode
    opts.inPurgeable = true; // Tell to gc that whether it needs free
    opts.inInputShareable = true; // Which kind of reference will be used to
    BitmapFactory.decodeFile(fPath, opts);
    final int REQUIRED_SIZE = 200;
    int scale = 1;
    if (opts.outHeight > REQUIRED_SIZE || opts.outWidth > REQUIRED_SIZE) {
        final int heightRatio = Math.round((float) opts.outHeight / (float) REQUIRED_SIZE);
        final int widthRatio = Math.round((float) opts.outWidth / (float) REQUIRED_SIZE);
        scale = heightRatio < widthRatio ? heightRatio : widthRatio;//
    }
    Log.i("scale", "scal =" + scale);
    opts.inJustDecodeBounds = false;
    opts.inSampleSize = scale;
    Bitmap bm = BitmapFactory.decodeFile(fPath, opts).copy(Config.ARGB_8888, false);
    return bm;
}