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 getBitmapFromDisk(String url, Context ctx) {

    Bitmap defautBitmap = null;/*from w  w  w .  j  a  va  2  s .  com*/
    try {
        String filename = constructFileName(url);
        File filePath = new File(ctx.getCacheDir(), filename);

        if (filePath.exists() && filePath.isFile() && !filePath.isDirectory()) {
            FileInputStream fi;
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inPreferredConfig = Config.RGB_565;
            fi = new FileInputStream(filePath);
            defautBitmap = BitmapFactory.decodeStream(fi, null, opts);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();

    } catch (Exception e) {

    } catch (OutOfMemoryError e) {

    }

    return defautBitmap;
}

From source file:Main.java

public static Bitmap decodeFile(String path, int desWidth, int desHeight) {
    Bitmap result = null;/*w w w  . ja  v a2 s  .  co m*/
    File f = null;
    FileInputStream fileInputStream = null;
    try {
        f = new File(path);
        if (!f.exists()) {
            return null;
        }
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        fileInputStream = new FileInputStream(f);
        BitmapFactory.decodeStream(fileInputStream, null, o);

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < desWidth || height_tmp / 2 < desHeight)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        result = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
        logE(TAG, "error:" + e.getStackTrace());
    } finally {
        if (f != null) {
            f = null;
        }
        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            fileInputStream = null;
        }
    }
    return result;
}

From source file:Main.java

@SuppressLint("NewApi")
public static Bitmap decodeSampledBitmap(Uri uri, int reqWidth, int reqHeight, Activity act) {

    // First decode with inJustDecodeBounds=true to check dimensions
    InputStream is;//  ww w  .  jav a  2  s.  c  o  m
    try {
        is = act.getApplicationContext().getContentResolver().openInputStream(uri);
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, options);
        is.close(); //consider use is.mark and is.reset instead [TODO]

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

        // Decode bitmap with inSampleSize set
        //open input stream again
        is = act.getApplicationContext().getContentResolver().openInputStream(uri);
        options.inJustDecodeBounds = false;
        Bitmap ret = BitmapFactory.decodeStream(is, null, options);
        is.close();
        return ret;
    } catch (FileNotFoundException e) {
        Log.v(TAG, "File not found:" + uri.toString());
        e.printStackTrace();
    } catch (IOException e) {
        Log.v(TAG, "I/O exception with file:" + uri.toString());
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/** Gets Bitmap from inputStream downsampled.
 *
 * @param bis//from w w  w  .  ja v a 2 s.c o  m
 * @param reqWidth
 * @param reqHeight
 * @return
 */
public static Bitmap decodeSampledBitmapFromStream(InputStream bis, int reqWidth, int reqHeight) {

    InputStream is = new BufferedInputStream(bis);

    try {
        is.mark(is.available());
    } catch (IOException e) {
        e.printStackTrace();
    }

    // First decode with inJustDecodeBounds=true to check dimensions
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap bmp0 = BitmapFactory.decodeStream(is, null, options);

    try {
        is.reset();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Calculate inSampleSize
    //options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeStream(is, null, options);
    return bmp;
}

From source file:Main.java

public static Bitmap bitmapFromUri(Context context, Uri photoUri) throws FileNotFoundException, IOException {
    InputStream is = context.getContentResolver().openInputStream(photoUri);
    BitmapFactory.Options dbo = new BitmapFactory.Options();
    dbo.inJustDecodeBounds = true;/*from ww  w.  ja v  a 2s .com*/
    BitmapFactory.decodeStream(is, null, dbo);
    is.close();

    int rotatedWidth, rotatedHeight;

    int orientation = 0;

    if (photoUri.toString().contains("content:/")) {
        orientation = getOrientation(context, photoUri);
        Log.i("Photo Editor", "Orientation: " + orientation);
    } else {
        int orientationFormExif = getOrientationFromExif(photoUri, context);
        orientation = decodeExifOrientation(orientationFormExif);
        Log.i("Photo Editor", "Orientation form Exif: " + orientation);
    }

    if (orientation == 90 || orientation == 270) {
        rotatedWidth = dbo.outHeight;
        rotatedHeight = dbo.outWidth;
    } else {
        rotatedWidth = dbo.outWidth;
        rotatedHeight = dbo.outHeight;
    }

    Bitmap srcBitmap = readScaledBitmapFromUri(photoUri, context, rotatedWidth, rotatedHeight);

    srcBitmap = setProperOrientation(orientation, srcBitmap);
    return srcBitmap;
}

From source file:Main.java

public static Bitmap decodeFileAndResize(File f, int requiredSize) {
    try {//from w ww . ja  va  2  s.  c o  m

        int rotation = 0;
        try {
            ExifInterface exif = new ExifInterface(f.getAbsolutePath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            rotation = getRotation(orientation);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = requiredSize;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        // decode with inSampleSize
        return createScaledBitmap(f, rotation, scale);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Determines if the stream contains a valid image.
 *
 * @param inputStream An input stream.//from www  .  jav  a  2  s .  c  o  m
 * @return {@code true} if the stream contains a valid image; {@code false} if not.
 */
public static boolean hasImageContent(InputStream inputStream) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(inputStream, null, options);
    return options.outMimeType != null;
}

From source file:Main.java

/** Returns a Bitmap from the given URI that may be scaled by an integer factor to reduce its size,
 * so that its width and height are no greater than the corresponding parameters. The scale factor
 * will be a power of 2./*  w  w w .j av a  2 s  .com*/
 */
public static Bitmap scaledBitmapFromURIWithMaximumSize(Context context, Uri imageURI, int width, int height)
        throws FileNotFoundException {
    BitmapFactory.Options options = computeBitmapSizeFromURI(context, imageURI);
    options.inJustDecodeBounds = false;

    int wratio = powerOf2GreaterOrEqual(1.0 * options.outWidth / width);
    int hratio = powerOf2GreaterOrEqual(1.0 * options.outHeight / height);
    options.inSampleSize = Math.max(wratio, hratio);

    return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options);
}

From source file:Main.java

public static Bitmap getBitmapFromFile(File file, int w) {
    FileInputStream fileInputStream = null;
    try {// ww w. ja v a2s.  c  o m
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        if (file == null || !file.exists()) {
            return null;
        } else if (file.length() == 0) {
            file.delete();
            return null;
        }
        fileInputStream = new FileInputStream(file);
        BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
        int be = getSampleSize(opts.outWidth, w);
        opts.inSampleSize = be;
        opts.inJustDecodeBounds = false;
        opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeStream(fileInputStream, null, opts);

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

From source file:Main.java

/** load the Bitmap from assets or sdcard_dir
 * @param context/* w w w  .  ja  v a 2 s. c om*/
 * @param outsideFileName
 * @param op
 * @param useSdcard
 * @param sdcard_assetdir_path
 * @return
 */
public static Bitmap loadBitmap(Context context, String outsideFileName, BitmapFactory.Options op,
        boolean useSdcard, String sdcard_assetdir_path) {
    AssetManager am = context.getAssets();

    Bitmap retval = null;
    synchronized (sCacheMap) {
        if (sCacheMap.get(outsideFileName) != null) {
            retval = sCacheMap.get(outsideFileName);
            return retval;
        }
    }

    try {
        retval = BitmapFactory.decodeStream(am.open(outsideFileName), null, op);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (useSdcard) {
        Bitmap _tmp = BitmapFactory.decodeFile(sdcard_assetdir_path + outsideFileName, op);
        if (_tmp != null) {
            retval = _tmp;
        }
    }

    if (retval != null) {
        synchronized (sCacheMap) {
            sCacheMap.put(outsideFileName, retval);
        }

    }

    return retval;
}