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 safeDecodeBitmap(String bmpFile, BitmapFactory.Options options) {
    BitmapFactory.Options optsTmp = options;
    if (optsTmp == null) {
        optsTmp = new BitmapFactory.Options();
        optsTmp.inSampleSize = 1;/*from   w  ww.  j  a va2 s .  c  o m*/
    }

    Bitmap bmp = null;
    FileInputStream fis = null;

    int i = 1;
    while (i < 5) {
        try {
            fis = new FileInputStream(bmpFile);
            bmp = BitmapFactory.decodeStream(fis, null, optsTmp);
            break;
        } catch (OutOfMemoryError error) {
            error.printStackTrace();

            optsTmp.inSampleSize *= 2;

            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            ++i;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            break;
        }
    }

    return bmp;
}

From source file:Main.java

public static int[] getImageSize(String filePath) {
    int[] size = new int[2];

    FileInputStream fis = null;/*from www .ja va  2 s  .  c o  m*/
    try {
        fis = new FileInputStream(new File(filePath));

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        size[0] = size[1] = -1;
        return size;
    }
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(fis, null, options);
        size[0] = options.outWidth;
        size[1] = options.outHeight;
    } catch (Exception e) {
        e.printStackTrace();
        size[0] = size[1] = -1;
        return size;
    } finally {
        try {
            if (fis != null) {
                fis.close();
                fis = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return size;
}

From source file:Main.java

public static Bitmap decodeBitmapFromAssets(Context context, String resName) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inPurgeable = true;//from w  w w. j av  a2 s . co m
    options.inInputShareable = true;
    InputStream in = null;
    try {
        //in = AssetsResourcesUtil.openResource(resName);
        in = context.getAssets().open(resName);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return BitmapFactory.decodeStream(in, null, options);
}

From source file:Main.java

/**
 *
 * @param stream/*  w  w  w  .j  a v  a  2s.c  om*/
 * @param dip
 * @return
 */
public static Bitmap decodeStream(InputStream stream, float dip) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    if (dip != 0.0F) {
        options.inDensity = (int) (160.0F * dip);
    }
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    setInNativeAlloc(options);
    try {
        Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
        return bitmap;
    } catch (OutOfMemoryError e) {
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        setInNativeAlloc(options);
        try {
            Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
            return bitmap;
        } catch (OutOfMemoryError e2) {
        }
    }
    return null;
}

From source file:Main.java

public static Bitmap urlToBitmap(String siteUrl, int requireSize) {
    //Log.d(LOG_TAG, "call the urlToBitmap " + ++callTime); // called 8 times..

    if (siteUrl == null) {
        //Log.d(LOG_TAG, "the url is null, throw Exception");
        return null;
    }//  w  ww .j  a va 2s . co  m

    URL url;
    try {
        url = new URL(siteUrl);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(url.openStream(), null, options);

        // determinte the scale size
        int scale = 1;
        //Log.d(Constant.LOG_TAG, "the outHeight is " + options.outHeight + ", the outWidth is " + options.outWidth);

        scale = computeSampleSize(options, -1, requireSize * requireSize);
        options.inJustDecodeBounds = false;
        //Log.d(Constant.LOG_TAG, "the scale is " + scale);
        options.inSampleSize = scale;
        Bitmap bitmap = BitmapFactory.decodeStream(url.openStream(), null, options);
        //Log.d(LOG_TAG, "get the bitmap in urltobitmap " + callTime);
        return bitmap;
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //Log.d(LOG_TAG, "return null in urltobitmap " + callTime);
    return null;

}

From source file:Main.java

public static Point getBitmapSize(InputStream is) {
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inJustDecodeBounds = true;/*from   ww  w .j a v a  2  s .  c  o m*/
    BitmapFactory.decodeStream(is, null, opt);
    int x = opt.outWidth;
    int y = opt.outHeight;
    Point p = new Point(x, y);
    return p;
}

From source file:Main.java

public static Bitmap getThumbnail(Uri uri, Context context) throws FileNotFoundException, IOException {
    InputStream input = context.getContentResolver().openInputStream(uri);

    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
    onlyBoundsOptions.inJustDecodeBounds = true;
    onlyBoundsOptions.inDither = true;//optional
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
    BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
    input.close();/*from   w ww  .  j av a  2s. c  o m*/
    if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
        return null;

    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
            : onlyBoundsOptions.outWidth;

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

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
    bitmapOptions.inDither = true;//optional
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
    input = context.getContentResolver().openInputStream(uri);
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
    input.close();
    return bitmap;
}

From source file:Main.java

public static Bitmap safeDecodeBimtapFile(String bmpFile, BitmapFactory.Options opts) {

    BitmapFactory.Options optsTmp = opts;
    if (optsTmp == null) {
        optsTmp = new BitmapFactory.Options();
        optsTmp.inSampleSize = 1;//from   w w w  .j a  va2s.c  o m
    }

    Bitmap bmp = null;
    FileInputStream input = null;

    final int MAX_TRIAL = 5;
    for (int i = 0; i < MAX_TRIAL; ++i) {
        try {
            input = new FileInputStream(bmpFile);
            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();
            }
        } catch (FileNotFoundException e) {
            break;
        }
    }

    return bmp;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, int reqHeight) {

    if (path == null) {
        return null;
    }//from   w w  w.j a  v  a 2  s .c  o  m

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    File file = new File(path);
    InputStream in = null;
    try {
        in = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
    BitmapFactory.decodeStream(in, null, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    options.inJustDecodeBounds = false;

    try {
        in = new FileInputStream(file);

    } catch (FileNotFoundException e) {

        e.printStackTrace();
        return null;
    }

    return BitmapFactory.decodeStream(in, null, options);
}

From source file:Main.java

/**
 * Decode a file from the path and return a bitmap
 * @param uri/*from   w w w .j  a va 2s .  co  m*/
 * @param context
 * @return
 */
public static Bitmap decodeFileFromPath(Uri uri, Context context) {

    InputStream in = null;
    try {
        in = context.getContentResolver().openInputStream(uri);

        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(in, null, o);
        in.close();

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

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        in = context.getContentResolver().openInputStream(uri);
        Bitmap b = BitmapFactory.decodeStream(in, null, o2);
        in.close();

        return b;

    } catch (FileNotFoundException e) {
        //e.printStackTrace();
    } catch (IOException e) {
        //e.printStackTrace();
    }
    return null;
}