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

private static Bitmap decodeBitmapWithSize(String pathName, int width, int height, boolean useBigger) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  w ww  .jav  a 2  s.  c  o  m*/
    options.inInputShareable = true;
    options.inPurgeable = true;
    BitmapFactory.decodeFile(pathName, options);

    int decodeWidth = width, decodeHeight = height;
    final int degrees = getImageDegrees(pathName);
    if (degrees == 90 || degrees == 270) {
        decodeWidth = height;
        decodeHeight = width;
    }

    if (useBigger) {
        options.inSampleSize = (int) Math.min(((float) options.outWidth / decodeWidth),
                ((float) options.outHeight / decodeHeight));
    } else {
        options.inSampleSize = (int) Math.max(((float) options.outWidth / decodeWidth),
                ((float) options.outHeight / decodeHeight));
    }

    options.inJustDecodeBounds = false;
    Bitmap sourceBm = BitmapFactory.decodeFile(pathName, options);
    return imageWithFixedRotation(sourceBm, degrees);
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static int[] getBitmapSize(File file, int targetWidth, int targetHeight) {
    if (null != file && file.exists() && file.isFile() && file.canRead()) {
        String path = file.getAbsolutePath();
        Options opts = new Options();
        if (targetWidth > 0 && targetHeight > 0) {
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, opts);
            opts.inSampleSize = computeSampleSize(opts, Math.min(targetWidth, targetHeight),
                    targetWidth * targetHeight);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                opts.inInputShareable = true;
                opts.inPurgeable = true;
            }//w  ww  .j a v  a2s.  c  o m
        }
        BitmapFactory.decodeFile(path, opts);
        return new int[] { opts.outWidth, opts.outHeight };
    }
    return null;
}

From source file:Main.java

public static Bitmap getSmallBitmap(String filePath, int w, int h) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  w w w. j ava 2  s.c o m
    BitmapFactory.decodeFile(filePath, options);
    options.inSampleSize = calculateInSampleSize(options, w, h);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}

From source file:Main.java

/**
 * @author Casper Rasmussen - 2012/* www . j a  v  a  2 s  .com*/
 * Use this method to get byte[] from a file of a given path.
 * @param filePath
 * @param compressRate - 0-100%, 100% means no compression
 * @param mCompressFormat - CompressFormat.PNG, CompressFormat.JPEG etc.
 * @return byte[]
 */
public static byte[] filePathToByteArray(String filePath, int compressRate, CompressFormat mCompressFormat) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    Bitmap bitmapFromFile = BitmapFactory.decodeFile(filePath, options);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmapFromFile.compress(mCompressFormat, compressRate, stream);
    byte[] byteArray = stream.toByteArray();

    return byteArray;
}

From source file:Main.java

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

    final int rotation = getRotation(fileName);

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* w w w. j av  a2 s  . com*/
    BitmapFactory.decodeFile(fileName, options);

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap thumb = null;
    try {
        thumb = BitmapFactory.decodeFile(fileName, options);
        return rotate(thumb, rotation);
    } finally {
        if (rotation != 0 && thumb != null) {
            thumb.recycle();
        }
    }
}

From source file:Main.java

public static Bitmap getBitmapFromFile(File file, int w) {
    FileInputStream fileInputStream = null;
    try {//from w w  w. j ava  2s.  com
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        if (file == null || !file.exists()) {
            return null;
        } else if (file.length() == 0) {
            file.delete();
            return null;
        }
        fileInputStream = new FileInputStream(file);
        BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
        int be = getSampleSize(opts.outWidth, w);
        opts.inSampleSize = be;
        opts.inJustDecodeBounds = false;
        opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeStream(fileInputStream, null, opts);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 *
 * @param picturePath complete path name for the file to be decoded.
 * @return Bitmap instance/*from www.  j  ava  2 s .com*/
 */
public static Bitmap pathToBitmap(String picturePath) {
    BitmapFactory.Options opts = new BitmapFactory.Options();

    opts.inDither = false; //Disable Dithering mode
    opts.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
    opts.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
    opts.inTempStorage = new byte[32 * 1024];

    return BitmapFactory.decodeFile(picturePath, opts);
}

From source file:Main.java

public static Bitmap getBitmapFromPath(String imgPath) {
    Bitmap bm = null;
    bm = BitmapFactory.decodeFile(imgPath, getDefOption());
    return bm;
}

From source file:Main.java

/**
 * Get a BitmapDrawable from a local file that is scaled down
 * to fit the current Window size.//from   w  w  w.j a  v  a 2 s .c o  m
 */
@SuppressWarnings("deprecation")
public static BitmapDrawable getScaledDrawable(Activity a, String path) {
    Display display = a.getWindowManager().getDefaultDisplay();
    float destWidth = display.getWidth();
    float destHeight = display.getHeight();

    // read in the dimensions of the image on disk
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    float srcWidth = options.outWidth;
    float srcHeight = options.outHeight;

    int inSampleSize = 1;
    if (srcHeight > destHeight || srcWidth > destWidth) {
        if (srcWidth > srcHeight) {
            inSampleSize = Math.round((float) srcHeight / ((float) destHeight) * 3);
        } else {
            inSampleSize = Math.round((float) srcWidth / ((float) destWidth) * 3);
        }
    }

    options = new BitmapFactory.Options();
    options.inSampleSize = inSampleSize;

    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    return new BitmapDrawable(a.getResources(), bitmap);
}

From source file:Main.java

public static Bitmap decodeBitmapFromPath(String path, int reqWidth) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* w w  w  .  j  a  v  a 2  s  . c o  m*/
    BitmapFactory.decodeFile(path, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth);

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