List of usage examples for android.graphics BitmapFactory decodeStream
@Nullable public static Bitmap decodeStream(@Nullable InputStream is, @Nullable Rect outPadding, @Nullable Options opts)
From source file:Main.java
public static Bitmap createOriginalBitmap(InputStream is) { Bitmap bm = null;//from w w w .j a v a 2s .c om try { bm = BitmapFactory.decodeStream(is, null, null); } catch (OutOfMemoryError e) { System.gc(); } return bm; }
From source file:Main.java
/** * Decodes an InputStream to Bitmap resizing the image to be <code>inSampleSize</code> times smaller then the original. * @param inputStream an input stream to an image * @param inSampleSize how much smaller that the image will be, for example, <code>inSampleSize</code> equals 2 will return an image 1/2 the size of the original * @return the resized Bitmap/* w w w.jav a 2 s . c o m*/ */ public static Bitmap decodeSampledBitmapFromInputStream(InputStream inputStream, int inSampleSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = inSampleSize; return BitmapFactory.decodeStream(inputStream, null, options); }
From source file:Main.java
public static Bitmap loadMpoBitmapFromFile(File file, long offset, int maxWidth, int maxHeight) throws IOException { // First, decode the width and height of the image, so that we know how much to scale // it down when loading it into our ImageView (so we don't need to do a huge allocation). BitmapFactory.Options opts = new BitmapFactory.Options(); InputStream fs = null;//from w w w .jav a2 s . c o m try { fs = new BufferedInputStream(new FileInputStream(file)); fs.skip(offset); opts.inJustDecodeBounds = true; BitmapFactory.decodeStream(fs, null, opts); } finally { if (fs != null) { try { fs.close(); } catch (IOException e) { // don't worry } } } int scale = 1; if (opts.outHeight > maxHeight || opts.outWidth > maxWidth) { scale = (int) Math.pow(2, (int) Math .round(Math.log(maxWidth / (double) Math.max(opts.outHeight, opts.outWidth)) / Math.log(0.5))); } if ((opts.outHeight <= 0) || (opts.outWidth <= 0)) { return null; } // Decode the image for real, but now with a sampleSize. // We have to reopen the file stream, and re-skip to the correct offset, since // FileInputStream doesn't support reset(). Bitmap bmp = null; fs = null; try { fs = new BufferedInputStream(new FileInputStream(file)); fs.skip(offset); BitmapFactory.Options opts2 = new BitmapFactory.Options(); opts2.inSampleSize = scale; bmp = BitmapFactory.decodeStream(fs, null, opts2); } finally { if (fs != null) { try { fs.close(); } catch (IOException e) { // don't worry } } } return bmp; }
From source file:Main.java
/** Returns a BitmapFactory.Options object containing the size of the image at the given URI, * without actually loading the image./*from w w w. j a v a 2 s .c o m*/ */ public static BitmapFactory.Options computeBitmapSizeFromURI(Context context, Uri imageURI) throws FileNotFoundException { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options); return options; }
From source file:Main.java
/** * Decodes from an input stream that optimally supports mark and reset * operations. If a maximum width and/or height are specified then the * passed stream must support mark and reset so that the bitmap can be down * sampled properly. If the width and/or height are specified and the input * stream does not support mark and reset, then an IllegalArgumentException * will be throw./* w w w. j a va 2 s. c om*/ */ public static Bitmap decodeSampledBitmapFromStream(InputStream inputStream, int width, int height) { if ((width != 0 || height != 0) && !inputStream.markSupported()) { throw new IllegalArgumentException( "Bitmap decoding requires an input stream that supports " + "mark and reset"); } // Set a mark for reset. Since we have no idea of the size of this // image, just set the maximum value possible. inputStream.mark(Integer.MAX_VALUE); // First decode with inJustDecodeBounds=true to check dimensions. final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, options); // Reset the stream for the actual decoding phase. try { inputStream.reset(); } catch (IOException e) { Log.e(TAG, "Failed to reset input stream during bitmap decoding"); return null; } // If either width or height is passed in as 0, then use the actual // stored image dimension. if (width == 0) { width = options.outWidth; } if (height == 0) { height = options.outHeight; } // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, width, height); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeStream(inputStream, null, options); }
From source file:Main.java
/** * Returns a Bitmap from the given URI that may be scaled by an integer factor to reduce its size, * while staying as least as large as the width and height parameters. *//*from w w w.j a va 2 s .c om*/ @Nullable public static Bitmap scaledBitmapFromURIWithMinimumSize(Context context, Uri imageURI, int width, int height) throws FileNotFoundException { BitmapFactory.Options options = computeBitmapSizeFromURI(context, imageURI); options.inJustDecodeBounds = false; float wratio = 1.0f * options.outWidth / width; float hratio = 1.0f * options.outHeight / height; options.inSampleSize = (int) Math.min(wratio, hratio); return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options); // return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI)); }
From source file:Main.java
/** * See: http://stackoverflow.com/questions/477572/android-strange * -out-of-memory-issue-while-loading-an-image * -to-a-bitmap-object/823966#823966 * Thanks to StackOverflow user named Fedor. *//*from ww w .j a v a 2 s. c o m*/ public static Bitmap decodeFile(File f, int size) { Bitmap b = null; try { BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; FileInputStream fis = new FileInputStream(f); BitmapFactory.decodeStream(fis, null, o); fis.close(); int scale = 1; if (o.outHeight > size || o.outWidth > size) { scale = (int) Math.pow(2.0, (int) Math .round(Math.log(size / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inTempStorage = new byte[32 * 1024]; o2.inPurgeable = true; o2.inSampleSize = scale; fis = new FileInputStream(f); b = BitmapFactory.decodeStream(fis, null, o2); fis.close(); } catch (IOException e) { } return b; }
From source file:Main.java
/** * Decodifica ottimizzata per la memoria dei bitmap * /*from w ww .jav a 2 s.c o m*/ * @param uri * URI bitmap * @param reqWidth * Larghezza richiesta * @param reqHeight * Altezza richiesta * @return * @throws FileNotFoundException */ public static Bitmap decodeSampledFromUri(Context mContext, Uri uri, int reqWidth, int reqHeight) throws FileNotFoundException { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(mContext.getContentResolver().openInputStream(uri), null, options); // Setting decode options options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; // Bitmap is now decoded for real using calculated inSampleSize Bitmap bmp = BitmapFactory.decodeStream(mContext.getContentResolver().openInputStream(uri), null, options); return bmp; }
From source file:Main.java
public static BitmapFactory.Options getBitmapOptionsForSubSampling(final InputStream is) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;// ww w. ja va2s.c o m options.inPurgeable = true; BitmapFactory.decodeStream(is, null, options); return options; }
From source file:Main.java
public static Bitmap decodeStream(InputStream is, int reqWidth, int reqHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); // Determine insample size opts.inJustDecodeBounds = true;//from ww w .ja v a2 s. c o m BitmapFactory.decodeStream(is, null, opts); opts.inSampleSize = calculateInSampleSize(opts, reqWidth, reqHeight); // Decode the bitmap, regionally if necessary Bitmap bitmap = null; opts.inJustDecodeBounds = false; Rect rect = getCropRectIfNecessary(opts, reqWidth, reqHeight); try { if (rect != null) { BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false); bitmap = decoder.decodeRegion(rect, opts); } else { bitmap = BitmapFactory.decodeStream(is, null, opts); } } catch (IOException e) { Log.e(TAG, "Unable to decode bitmap from stream", e); } return bitmap; }