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 createBitmapFromUri(Context context, Uri uri) {

    Bitmap largeImage = null;//from  w w  w  .  jav  a  2  s .  co m
    try {
        InputStream iStream = context.getContentResolver().openInputStream(uri);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        largeImage = BitmapFactory.decodeStream(iStream, null, options);
        iStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return largeImage;
}

From source file:Main.java

public static final Bitmap getBitmapFromUri(Context context, Uri uri) {
    if (uri == null)
        return null;
    try {//from   w w w .j  a va2 s  . c o m
        InputStream is = context.getContentResolver().openInputStream(uri);
        BitmapFactory.Options imageOptions = new BitmapFactory.Options();
        Bitmap bmp = BitmapFactory.decodeStream(is, null, imageOptions);
        return bmp;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getSimilarBitmap(File f, int width, int height) {
    try {//from w  ww . jav a2  s  .  com
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream stream1 = new FileInputStream(f);
        BitmapFactory.decodeStream(stream1, null, o);
        stream1.close();

        float widthScale = o.outWidth / width;
        float heightScale = o.outHeight / height;
        float scale = widthScale > heightScale ? widthScale : heightScale;
        if (scale > 8) {
            o.inSampleSize = 8;
        } else if (scale > 6) {
            o.inSampleSize = 6;
        } else if (scale > 4) {
            o.inSampleSize = 4;
        } else if (scale > 2) {
            o.inSampleSize = 2;
        } else {
            o.inSampleSize = 1;
        }
        o.inJustDecodeBounds = false;
        FileInputStream stream2 = new FileInputStream(f);
        Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o);
        stream2.close();
        return bitmap;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static Bitmap readBitMap(Context context, int resId) {
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inPreferredConfig = Bitmap.Config.RGB_565;
    opt.inPurgeable = true;//from   ww  w .  jav a 2  s .  c  o m
    opt.inInputShareable = true;
    InputStream is = context.getResources().openRawResource(resId);
    return BitmapFactory.decodeStream(is, null, opt);
}

From source file:Main.java

public static Bitmap revitionImageSize(String path) {
    Bitmap bitmap = null;/* w  w w .  ja va2s  .  com*/
    try {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        int i = 0;

        while (true) {
            if ((options.outWidth >> i <= 1000) && (options.outHeight >> i <= 1000)) {
                in = new BufferedInputStream(new FileInputStream(new File(path)));
                options.inSampleSize = (int) Math.pow(2.0D, i);
                options.inJustDecodeBounds = false;
                bitmap = BitmapFactory.decodeStream(in, null, options);
                break;
            }
            i += 1;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return bitmap;
}

From source file:Main.java

/**
 *
 * @param path/*from www  .j  a  v a2  s.  co m*/
 * @return
 */
public static Bitmap getBitmapFromSDCard(String path) {
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(path);
        if (fis != null) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2;
            return BitmapFactory.decodeStream(fis, null, options);
        }
    } catch (Exception e) {

    }
    return null;
}

From source file:Main.java

public static int[] getImageWidhtHeightFromFilePath(String filePath) {
    try {/*from w  w  w  .ja  v  a2  s.  com*/
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        File imageFile = new File(filePath);
        InputStream is = new FileInputStream(imageFile);
        BitmapFactory.decodeStream(is, null, options);
        return new int[] { options.outWidth, options.outHeight };
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap readBitmap(final String path) {
    try {//from   w w w . j a va2  s. c o m
        FileInputStream stream = new FileInputStream(new File(path + "test.jpg"));
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 8;
        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

public static Bitmap loadImageFromStorage(String path) {
    Bitmap bmp = null;/*from w ww  .ja  v a 2  s . c o m*/
    try {
        File f = new File(path);

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        //BitmapFactory.decodeResource(res, resId, options);
        BitmapFactory.decodeStream(new FileInputStream(f), null, options);

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

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
        //BitmapFactory.decodeS
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return bmp;

}

From source file:Main.java

public static Bitmap byteToBitmap(byte[] imgByte) {
    InputStream input = null;//from  ww  w.  j  ava2  s. co m
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;
    input = new ByteArrayInputStream(imgByte);
    SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options));
    bitmap = (Bitmap) softRef.get();
    if (imgByte != null) {
        imgByte = null;
    }

    try {
        if (input != null) {
            input.close();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bitmap;
}