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 decodeFileRestrictDataSize(String path, int maxDataSize) {
    Options options = getBitmapOptionsWithSize(path);
    int width = options.outWidth;
    int height = options.outHeight;
    int scale = 1;
    while (width * height * 4 > maxDataSize) {
        scale *= 2;//from  www  .ja v a  2 s . c o m
        width /= 2;
        height /= 2;
    }
    options.inSampleSize = scale;
    return BitmapFactory.decodeFile(path, options);
}

From source file:Main.java

/**
 * Get Bitmap from file/* w  ww .  j  av a2 s  .co m*/
 *
 * @param imageFile - Image file
 * @return Generated Bitmap
 */
public static Bitmap getBitmapFromFile(File imageFile) {
    BitmapFactory.Options bitmapDecodeOptions = new BitmapFactory.Options();
    bitmapDecodeOptions.inTempStorage = new byte[KILOBYTES];
    bitmapDecodeOptions.inSampleSize = SAMPLE_SIZE;
    return BitmapFactory.decodeFile(imageFile.getAbsolutePath(), bitmapDecodeOptions);
}

From source file:Main.java

@SuppressWarnings("unused")
private static Bitmap readBitMap(String filePath) throws FileNotFoundException {
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inPreferredConfig = Config.RGB_565;
    opt.inPurgeable = true;/*from ww  w  .j  a v  a2 s .c o  m*/
    opt.inInputShareable = true;
    //      return BitmapFactory.decodeStream(new FileInputStream(file), null, opt);
    return BitmapFactory.decodeFile(filePath, opt);
}

From source file:Main.java

public static Bitmap getBitmap(String path, int requiredSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from   www . ja va2 s . c o m
    BitmapFactory.decodeFile(path, options);
    int scale = 1;

    while (options.outWidth / scale > requiredSize && options.outHeight / scale > requiredSize)
        scale *= 2;

    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inTempStorage = new byte[32 * 1024];

    Bitmap result = null;
    File file = new File(path);
    FileInputStream fs = null;
    try {
        fs = new FileInputStream(file);

        try {
            result = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
        } catch (OutOfMemoryError oom) {
            oom.printStackTrace();

            try {
                options.inSampleSize *= 4;
                result = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
            } catch (OutOfMemoryError oom1) {
                oom.printStackTrace();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fs != null)
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return result;
}

From source file:Main.java

public static BitmapDrawable getCachedIcon(Resources res, String packageName, int resId, Options opts) {
    int displayDpi = res.getDisplayMetrics().densityDpi;
    Bitmap bitmap = BitmapFactory.decodeFile(getCacheFilePath(packageName, resId), opts);
    if (bitmap == null) {
        return null;
    }/*from   w  w w  .j  av a2s. com*/
    bitmap.setDensity(displayDpi);
    return new BitmapDrawable(res, bitmap);
}

From source file:Main.java

/**
 * get image size by path//from www .ja  va2 s.  co  m
 */
public static Point getImageSize(String path) throws IOException {
    if (TextUtils.isEmpty(path)) {
        return null;
    }
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    if (path.startsWith("http")) {
        BitmapFactory.decodeStream(new URL(path).openStream(), null, options);
    } else {
        BitmapFactory.decodeFile(path, options);
    }
    return new Point(options.outWidth, options.outHeight);
}

From source file:Main.java

public static Bitmap getScaledBitmap(String path, int destWidth, int destHeight) {
    // read in the dimensions of the image on disk
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* ww  w .  j av a 2  s  . co  m*/
    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(srcHeight / destHeight);
        } else {
            inSampleSize = Math.round(srcWidth / destWidth);
        }
    }

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

    return BitmapFactory.decodeFile(path, options);
}

From source file:Main.java

/**
 * Get a bitmap from file and try to resize it to be 300x300 pixels.
 *
 * @param filepath Path to the image file.
 * @param reqWidth Requested width./*from w  w  w.j a  v a  2 s . c o  m*/
 * @param reqHeight Requested height.
 * @return Bitmap of image.
 */
public static Bitmap getResizedBitmap(String filepath, int reqWidth, int reqHeight) {
    Bitmap bitmap;

    // Decode bitmap to get current dimensions.
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filepath, options);

    // Calculate sample size of bitmap.
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap again, setting the new dimensions.
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFile(filepath, options);

    // Return resized bitmap.
    return bitmap;
}

From source file:Main.java

public static Bitmap decodeBitmapFromFile(String filename, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  w w w  .  j  a  v a2s . co m*/
    BitmapFactory.decodeFile(filename, options);

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

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

From source file:Main.java

public static void createScaledImage(String sourceFile, String destinationFile, int desiredWidth,
        int desiredHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  w  w w  .  j  a v  a  2  s.  co  m
    BitmapFactory.decodeFile(sourceFile, options);

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

    if (desiredWidth > srcWidth) {
        desiredWidth = srcWidth;
    }

    int inSampleSize = 1;
    while (srcWidth / 2 > desiredWidth) {
        srcWidth /= 2;
        srcHeight /= 2;
        inSampleSize *= 2;
    }

    float desiredScale = (float) desiredWidth / srcWidth;

    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inSampleSize = inSampleSize;
    options.inScaled = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(sourceFile, options);

    Matrix matrix = new Matrix();
    matrix.postScale(desiredScale, desiredScale);
    Bitmap scaledBitmap = Bitmap.createBitmap(sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(),
            sampledSrcBitmap.getHeight(), matrix, true);
    sampledSrcBitmap = null;

    try {
        FileOutputStream out = new FileOutputStream(destinationFile);
        scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, out);
        scaledBitmap = null;
    } catch (IOException e) {
        e.printStackTrace();
    }
}