Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.annotation.Nullable; import java.io.IOException; import java.io.InputStream; public class Main { public static Bitmap loadFromInputStream(InputStream input, @Nullable BitmapFactory.Options opts) { BitmapFactory.Options optsTmp = opts; if (optsTmp == null) { optsTmp = new BitmapFactory.Options(); optsTmp.inSampleSize = 1; } 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; } }