Code example for BufferedInputStream

Methods: decodeStream, mark, reset

0
	 * @param config          You can use this to change the number of bytes per pixel using various bitmap configurations. 
	 * @return A bitmap whose edges are equal to or less than minShrunkEdge in length. 
	 */ 
	public static Bitmap createLightweightScaledBitmapFromStream(InputStream is, int minShrunkWidth, int minShrunkHeight, Bitmap.Config config) {
 
		BufferedInputStream bis = new BufferedInputStream(is, 32 * 1024);
		try { 
			BitmapFactory.Options options = new BitmapFactory.Options();
			if (config != null) {
				options.inPreferredConfig = config;
			} 
 
			final BitmapFactory.Options decodeBoundsOptions = new BitmapFactory.Options();
			decodeBoundsOptions.inJustDecodeBounds = true;
			bis.mark(Integer.MAX_VALUE);
			BitmapFactory.decodeStream(bis, null, decodeBoundsOptions);
			bis.reset();
 
			final int width = decodeBoundsOptions.outWidth;
			final int height = decodeBoundsOptions.outHeight;
			Log.v("Original bitmap dimensions: %d x %d", width, height);
			int sampleRatio = Math.max(width / minShrunkWidth, height / minShrunkHeight);
			if (sampleRatio >= 2) {
				options.inSampleSize = sampleRatio;
			} 
			Log.v("Bitmap sample size = %d", options.inSampleSize);
 
			Bitmap ret = BitmapFactory.decodeStream(bis, null, options);
			Log.d("Sampled bitmap size = %d X %d", options.outWidth, options.outHeight);
			return ret;
		} catch (IOException e) {
			Log.e("Error resizing bitmap from InputStream.", e);
		} finally { 
			Util.ensureClosed(bis);
		} 
		return null; 
	} 
 
	/**