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

/**
 * Get Bitmap from Uri/*www  .j  a v a2s. c om*/
 *
 * @param uri Uri to get Bitmap
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
public static Bitmap getImageFromUri(Activity activity, Uri uri, File file) throws IOException {

    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
    onlyBoundsOptions.inJustDecodeBounds = true;
    onlyBoundsOptions.inDither = true;//optional
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
    if (file != null) {
        BitmapFactory.decodeFile(file.getAbsolutePath(), onlyBoundsOptions);
    } else {
        InputStream input = activity.getContentResolver().openInputStream(uri);
        BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
        input.close();
    }

    if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
        return null;

    float scale = activity.getResources().getDisplayMetrics().density;
    int pHeight = (int) (activity.getResources().getConfiguration().screenHeightDp * scale + 0.5f);
    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
            : onlyBoundsOptions.outWidth;

    double ratio = (originalSize > pHeight) ? (originalSize / pHeight) : 1.0;

    int REQUIRED_SIZE = activity.getResources().getDisplayMetrics().heightPixels / 2;

    /**/
    int Scale = 1;
    while (onlyBoundsOptions.outWidth / Scale / 2 >= REQUIRED_SIZE
            && onlyBoundsOptions.outHeight / Scale / 2 >= REQUIRED_SIZE) {
        Scale *= 2;
    }
    /**/

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = Scale;//getPowerOfTwoForSampleRatio(ratio);
    bitmapOptions.inDither = true;//optional
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional

    Bitmap bitmap;
    if (file != null) {
        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);
    } else {
        InputStream input = activity.getContentResolver().openInputStream(uri);
        bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
        input.close();
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap readBitmapFile(String aFileName, Options aOptions) {
    Bitmap bitmap = null;//from   w  w  w . j a  va  2 s .c  om
    File file = new File(aFileName);
    try {
        FileInputStream fis = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fis, null, aOptions);
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap load(ContentResolver contentResolver, Uri uri, int minSideLen, int maxSize) {

    Bitmap result = EMPTY_BITMAP;//  w ww  . j a  v  a  2  s .  c  o m
    try {
        Options opts = new Options();
        opts.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, opts);

        result = load(contentResolver.openInputStream(uri), computeSampleSize(opts, minSideLen, maxSize));

        if (isMediaUri(uri) && result != null && result != EMPTY_BITMAP) {
            result = rotateMediaImage(contentResolver, uri, result);
        }

    } catch (OutOfMemoryError e) {
        Log.e("LoadImage", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("LoadImage", e.getMessage(), e);
    }
    if (result == null) {
        result = EMPTY_BITMAP;
    }
    return result;
}

From source file:Main.java

public static int computeInSampleSize(InputStream inputStream, int requireWidth, int requireHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* w  ww  . jav  a  2s. com*/
    BitmapFactory.decodeStream(inputStream, null, options);
    return computeInSampleSize(options, requireWidth, requireHeight);
}

From source file:Main.java

static Bitmap decodeBitmap(Context context, Uri uri, BitmapFactory.Options options, int maxW, int maxH,
        int orientation, int pass) {

    Bitmap bitmap = null;//  w  ww  . j  a  v a2 s  .c o m
    Bitmap newBitmap = null;

    if (pass > 20) {
        return null;
    }

    InputStream stream = openInputStream(context, uri);
    if (null == stream)
        return null;

    try {
        // decode the bitmap via android BitmapFactory
        bitmap = BitmapFactory.decodeStream(stream, null, options);
        closeSilently(stream);

        if (bitmap != null) {
            newBitmap = resizeBitmap(bitmap, maxW, maxH, orientation);
            if (bitmap != newBitmap) {
                bitmap.recycle();
            }
            bitmap = newBitmap;
        }

    } catch (OutOfMemoryError error) {
        closeSilently(stream);
        if (null != bitmap) {
            bitmap.recycle();
        }
        options.inSampleSize += 1;
        bitmap = decodeBitmap(context, uri, options, maxW, maxH, orientation, pass + 1);
    }
    return bitmap;

}

From source file:Main.java

public static Bitmap decodeBitmap(Context context, InputStream is) {
    checkParam(context);/*  w w w.j av a  2 s .c om*/
    checkParam(is);
    return BitmapFactory.decodeStream(is, null, getBitmapOptions(context));
}

From source file:Main.java

public static Bitmap getImageBitmapFromAssetsFolderThroughImagePathName(Context context, String imagePathName,
        int reqWidth, int reqHeight) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;//ww  w  .ja v a 2s .c  o m
    Bitmap bitmap = null;
    AssetManager assetManager = context.getResources().getAssets();
    InputStream inputStream = null;

    try {
        inputStream = assetManager.open(imagePathName);
        inputStream.mark(Integer.MAX_VALUE);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    }

    try {
        if (inputStream != null) {
            BitmapFactory.decodeStream(inputStream, null, opts);
            inputStream.reset();
        } else {
            return null;
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    opts.inSampleSize = calculateInSampleSiez(opts, reqWidth, reqHeight);
    //         Log.d(TAG,""+opts.inSampleSize);
    opts.inJustDecodeBounds = false;
    opts.inPreferredConfig = Bitmap.Config.RGB_565;
    opts.inPurgeable = true;
    opts.inInputShareable = true;
    opts.inDither = false;
    opts.inTempStorage = new byte[512 * 1024];
    try {
        if (inputStream != null) {
            bitmap = BitmapFactory.decodeStream(inputStream, null, opts);
        } else {
            return null;
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    //Log.d(TAG,"w:"+bitmap.getWidth()+" h:"+bitmap.getHeight());
    if (bitmap != null) {
        try {
            bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
        } catch (OutOfMemoryError outOfMemoryError) {
            outOfMemoryError.printStackTrace();
            System.gc();
            return null;
        }
    }
    return bitmap;
}

From source file:Main.java

/**
 * Opens an InputStream for the person's photo and returns the photo as a
 * Bitmap. If the person's photo isn't present returns the
 * placeholderImageResource instead.//from ww w  . j  av a2  s  .c  o m
 * 
 * @param context
 *            the Context
 * @param id
 *            the id of the person
 * @param placeholderImageResource
 *            the image resource to use if the person doesn't have a photo
 * @param options
 *            the decoding options, can be set to null
 */
public static Bitmap loadContactPhoto(Context context, Uri contactUri, int placeholderImageResource,
        BitmapFactory.Options options) {

    if (contactUri == null) {
        return loadPlaceholderPhoto(placeholderImageResource, context, options);
    }

    InputStream stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri);

    Bitmap bm = stream != null ? BitmapFactory.decodeStream(stream, null, options) : null;
    if (bm == null) {
        bm = loadPlaceholderPhoto(placeholderImageResource, context, options);
    }

    return bm;
}

From source file:Main.java

public static final Bitmap getBitmapFromUri(Context context, Uri uri, int maxWidth, int maxHeight) {
    if (uri == null)
        return null;
    try {//ww  w. ja  v  a 2s .  c o m
        InputStream is = context.getContentResolver().openInputStream(uri);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // Set height and width in options, does not return an image and no resource taken
        BitmapFactory.decodeStream(is, null, options);
        int pow = 0;
        while (options.outHeight >> pow > maxHeight || options.outWidth >> pow > maxWidth) {
            pow += 1;
        }
        is.close();
        is = context.getContentResolver().openInputStream(uri);
        options.inSampleSize = 1 << pow;
        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeStream(is, null, options);

        return bmp;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

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