Example usage for android.graphics BitmapFactory decodeStream

List of usage examples for android.graphics BitmapFactory decodeStream

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeStream.

Prototype

@Nullable
public static Bitmap decodeStream(@Nullable InputStream is, @Nullable Rect outPadding, @Nullable Options opts) 

Source Link

Document

Decode an input stream into a bitmap.

Usage

From source file:Main.java

public static Bitmap decodeUriToCompressedBitmap(Uri selectedImage, Context context)
        throws FileNotFoundException {
    // The new size we want to scale to
    final int REQUIRED_SIZE = 140;

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;/*from ww w  .  java 2  s.  c  o  m*/
    BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o);

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o2);
}

From source file:Main.java

public static Bitmap decodeFileFromDrawable(int id, int maxSize, Context context) {
    Bitmap b = null;/* ww  w .  j  a  v a 2  s.c  o  m*/
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        InputStream inputStream = context.getResources().openRawResource(id);
        BitmapFactory.decodeStream(inputStream, null, o);
        inputStream.close();

        int scale = 1;
        if (o.outHeight > maxSize || o.outWidth > maxSize) {
            scale = (int) Math.pow(2, (int) Math
                    .round(Math.log(maxSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        inputStream = context.getResources().openRawResource(id);
        b = BitmapFactory.decodeStream(inputStream, null, o2);
        inputStream.close();
    } catch (IOException e) {
    }
    return b;
}

From source file:Main.java

public static Bitmap readBitmap(String path, int size) {
    try {//from   ww w.  j a va  2s  . c  o  m
        FileInputStream stream = new FileInputStream(new File(path));
        Options opts = new Options();
        opts.inSampleSize = size;
        opts.inPurgeable = true;
        opts.inInputShareable = true;
        Bitmap bitmap = BitmapFactory.decodeStream(stream, null, opts);
        return bitmap;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * get image size by path//from  w  ww .j a  va 2s .com
 */
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 decodeSampledBitmapStreamForSize(InputStream is, int reqWidth, int reqHeight)
        throws IOException {
    is.mark(is.available());// w  w w .  j  a  va2  s  .  co  m

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, options);

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

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

    is.reset();

    return BitmapFactory.decodeStream(is, null, options);
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromUrl(String urlStr, int reqWidth, int reqHeight)
        throws MalformedURLException, IOException {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   ww  w  . j a v a 2 s.  c  o  m*/
    BitmapFactory.decodeStream(getInputStreamFromUrl(urlStr), null, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inPurgeable = true;
    options.inInputShareable = true;
    return BitmapFactory.decodeStream(getInputStreamFromUrl(urlStr), null, options);
}

From source file:Main.java

/**
 * @param file/*from   w ww . ja v  a  2s. c  o  m*/
 * @param image_width
 * @param image_height
 * @return
 * @hide
 */
@SuppressWarnings("unused")
private static Bitmap decodeFile(File file, int image_width, int image_height) {
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(file), null, o);

        int REQUIRED_HEIGHT = image_height;
        int REQUIRED_WIDTH = image_width;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;

        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_WIDTH && height_tmp / 2 < REQUIRED_HEIGHT)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale++;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(file), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

From source file:Main.java

public static Bitmap decodeUriAsBitmap(Context context, Uri uri, BitmapFactory.Options options) {

    Bitmap result = null;/*from w  ww  .j a v  a  2  s .  c o m*/

    if (uri != null) {
        ContentResolver cr = context.getContentResolver();
        InputStream inputStream = null;
        try {
            inputStream = cr.openInputStream(uri);
            result = BitmapFactory.decodeStream(inputStream, null, options);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return result;
}

From source file:Main.java

public static final boolean checkImageAndSize(Context context, Uri uri, long bytes) {
    if (uri == null)
        return false;

    try {//from  ww w. j  a  v a  2 s  .c  o  m
        InputStream is = context.getContentResolver().openInputStream(uri);
        BitmapFactory.Options imageOptions = new BitmapFactory.Options();
        imageOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, imageOptions);
        //android.util.Log.d("test77", "Original Image Size: " + imageOptions.outWidth + " x " + imageOptions.outHeight);
        is.close();
        if (imageOptions.outWidth <= 0 || imageOptions.outHeight <= 0) {
            return false;
        }
        is = context.getContentResolver().openInputStream(uri);
        long fileSizeCounter = 0;
        long size;
        byte[] buf = new byte[8192];
        while ((size = is.read(buf, 0, buf.length)) != -1) {
            //android.util.Log.d("test77", "size="+size);
            fileSizeCounter += size;
        }
        is.close();
        if (fileSizeCounter > bytes) {
            return false;
        }
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static Bitmap getBitmapByPath(String filePath, BitmapFactory.Options opts) {
    FileInputStream fis = null;//ww w.j a  v  a  2 s . c o  m
    Bitmap bitmap = null;
    try {
        File file = new File(filePath);
        fis = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fis, null, opts);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    } finally {
        try {
            fis.close();
        } catch (Exception e) {
        }
    }
    return bitmap;
}