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 ReadBitmapById(Context context, int drawableId) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Config.ARGB_8888;
    options.inInputShareable = true;/*from  w ww  .j a  va 2 s  .  c om*/
    options.inPurgeable = true;
    InputStream stream = context.getResources().openRawResource(drawableId);
    Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
    return bitmap;
}

From source file:Main.java

public static Bitmap downloadImage(String url) {
    Bitmap bitmap = null;/*www.  ja va  2 s .  c o m*/
    InputStream stream = null;
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inSampleSize = 1;

    try {
        stream = getHttpConnection(url);
        bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
        stream.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return bitmap;
}

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.  com
    Bitmap bitmap = null;
    try {
        File file = new File(filePath);
        fis = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fis, null, opts);
    } catch (FileNotFoundException e) {
    } catch (OutOfMemoryError e) {
    } finally {
        try {
            fis.close();
        } catch (Exception e) {
        }
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap getBitmapByPath(String filePath, BitmapFactory.Options opts) {
    FileInputStream fis = null;/*  w ww .  ja  va 2s.  co m*/
    Bitmap bitmap = null;
    try {
        File file = new File(filePath);
        fis = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fis, null, opts);
    } catch (FileNotFoundException | OutOfMemoryError e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

public static BitmapDrawable getBitmapDrawableRes(Context context, int resId) {
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inPreferredConfig = Bitmap.Config.RGB_565;
    opt.inPurgeable = true;/* www  . j a va  2 s  .c om*/
    opt.inInputShareable = true;
    InputStream is = context.getResources().openRawResource(resId);
    Bitmap bitmap = BitmapFactory.decodeStream(is, null, opt);
    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new BitmapDrawable(context.getResources(), bitmap);
}

From source file:Main.java

public static Bitmap getBitmapAndScale(String url, int requiredSize) {
    Bitmap bm = null;//from  w w  w .java  2s  .c  o m
    try {
        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, o);
        // The new size we want to scale to

        // 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 < requiredSize || height_tmp / 2 < requiredSize)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        bm = BitmapFactory.decodeStream(is, null, o2);
        return bm;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return bm;

    }
}

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  w w  . j  a v a 2 s.co  m
    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

/**
 * //from   w  w  w. j a v a 2s .co m
 * @param file
 */
public static void decodeFile(File file) {
    Bitmap bitmap = null;

    try {
        // Decode image size
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        FileInputStream fileInputStream = new FileInputStream(file);
        BitmapFactory.decodeStream(fileInputStream, null, options);
        fileInputStream.close();

        int scale = 1;

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

        // Decode with inSampleSize
        BitmapFactory.Options options2 = new BitmapFactory.Options();
        options2.inSampleSize = scale;

        fileInputStream = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fileInputStream, null, options2);
        fileInputStream.close();

        FileOutputStream output = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, output);

        output.flush();
        output.close();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

From source file:Main.java

public static Bitmap decodeStream(Context paramContext, InputStream paramInputStream, Rect paramRect,
        BitmapFactory.Options paramOptions) {
    try {/*from  w  w w  .  ja va 2s. c  o  m*/
        Bitmap localBitmap = BitmapFactory.decodeStream(paramInputStream, paramRect, paramOptions);
        return localBitmap;
    } catch (OutOfMemoryError localOutOfMemoryError) {
    }
    return null;
}

From source file:Main.java

public static Bitmap loadFromInputStream(InputStream input, @Nullable BitmapFactory.Options opts) {

    BitmapFactory.Options optsTmp = opts;
    if (optsTmp == null) {
        optsTmp = new BitmapFactory.Options();
        optsTmp.inSampleSize = 1;/* ww w.ja  v a2 s  .  co  m*/
    }

    Bitmap bmp = null;

    final int MAX_TRIAL = 5;
    for (int i = 0; i < MAX_TRIAL; ++i) {
        try {
            bmp = BitmapFactory.decodeStream(input, null, opts);
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            optsTmp.inSampleSize *= 2;
            try {
                input.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    return bmp;
}