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

/**
 * 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.  co m*/
 */
@SuppressWarnings("deprecation")
static BitmapDrawable getScaledBitmap(String path, Activity a) {
    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(srcHeight / destHeight);
        } else {
            inSampleSize = Math.round(srcWidth / destWidth);
        }
    }

    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 decodeSampledBitmapByPath(Context context, String path) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    final int height = displayMetrics.heightPixels;
    final int width = displayMetrics.widthPixels;

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from w  w  w.ja  v  a2s  .  c o m*/
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);

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

From source file:Main.java

public static Bitmap readBitMap(String path) {
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inPreferredConfig = Bitmap.Config.RGB_565;
    opt.inPurgeable = true;//from  www . jav a 2  s  .  c om
    opt.inInputShareable = true;
    return BitmapFactory.decodeFile(path, opt);
}

From source file:Main.java

public static Bitmap getSmallBitmap(String filePath) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from w  w w  . j  av a 2  s.  c  o m
    BitmapFactory.decodeFile(filePath, options);

    // Calculate inSampleSize
    //      options.inSampleSize = calculateInSampleSize(options, 300, 300);
    options.inSampleSize = calculateInSampleSize(options, 300, 300, 300);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(filePath, options);
}

From source file:Main.java

/** @see http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue */
public static int getSampleSize(Context context, Uri uri, float maxSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  w  ww . jav a2 s.c om*/
    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        try {
            InputStream stream = context.getContentResolver().openInputStream(uri);
            BitmapFactory.decodeStream(stream, null, options);
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else
        BitmapFactory.decodeFile(uri.getPath(), options);
    int longSide = Math.max(options.outHeight, options.outWidth);
    return getSampleSize(longSide, maxSize);
}

From source file:Main.java

public static Bitmap getSampledBitmap(String filePath, int reqWidth, int reqHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   www .  j  av a  2s.  c o m*/
    BitmapFactory.decodeFile(filePath, options);
    int inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inSampleSize = inSampleSize;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}

From source file:Main.java

/**
 * Get Bitmap from Uri/*w  w w. j  av a 2  s  .  c o  m*/
 *
 * @param uri Uri to get Bitmap
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
public static Bitmap getImageFromUri(Activity activity, Uri uri, File file) throws IOException {

    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
    onlyBoundsOptions.inJustDecodeBounds = true;
    onlyBoundsOptions.inDither = true;//optional
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
    if (file != null) {
        BitmapFactory.decodeFile(file.getAbsolutePath(), onlyBoundsOptions);
    } else {
        InputStream input = activity.getContentResolver().openInputStream(uri);
        BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
        input.close();
    }

    if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
        return null;

    float scale = activity.getResources().getDisplayMetrics().density;
    int pHeight = (int) (activity.getResources().getConfiguration().screenHeightDp * scale + 0.5f);
    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
            : onlyBoundsOptions.outWidth;

    double ratio = (originalSize > pHeight) ? (originalSize / pHeight) : 1.0;

    int REQUIRED_SIZE = activity.getResources().getDisplayMetrics().heightPixels / 2;

    /**/
    int Scale = 1;
    while (onlyBoundsOptions.outWidth / Scale / 2 >= REQUIRED_SIZE
            && onlyBoundsOptions.outHeight / Scale / 2 >= REQUIRED_SIZE) {
        Scale *= 2;
    }
    /**/

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = Scale;//getPowerOfTwoForSampleRatio(ratio);
    bitmapOptions.inDither = true;//optional
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional

    Bitmap bitmap;
    if (file != null) {
        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);
    } else {
        InputStream input = activity.getContentResolver().openInputStream(uri);
        bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
        input.close();
    }

    return bitmap;
}

From source file:Main.java

public static Point getImageSize(String filePath) {
    try {/*w ww . j a  v  a  2s .  c  o m*/
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        return new Point(options.outWidth, options.outHeight);
    } catch (OutOfMemoryError ignored) {
    } catch (Exception ignored) {
    }

    return new Point(0, 0);
}

From source file:Main.java

public static Bitmap getSmallBmpFromFile(String filePath, int targetW, int targetH) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from w  w w  .  ja  va 2  s.c  o  m
    BitmapFactory.decodeFile(filePath, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, targetW, targetH);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFd(String pathName, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    //        options.inJustDecodeBounds = true;
    //        BitmapFactory.decodeFile(pathName, options);
    int sampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    //        if(sampleSize < 4)
    //        {/*from  w  w  w .  j  av a2  s.  c o m*/
    //            sampleSize = 4;
    //        }
    options.inSampleSize = sampleSize;
    options.inJustDecodeBounds = false;
    Bitmap src = BitmapFactory.decodeFile(pathName, options);
    return src;
}