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:com.socialize.util.ImageUtils.java

public byte[] scaleImage(Context context, Uri photoUri) throws IOException {

    BitmapFactory.Options dbo = new BitmapFactory.Options();
    dbo.inJustDecodeBounds = true;//from www .  j  a va2s . c  o  m
    InputStream is = getStreamToUri(context, photoUri);
    BitmapFactory.decodeStream(is, null, dbo);
    is.close();

    int rotatedWidth, rotatedHeight;
    int orientation = getOrientation(context, photoUri);

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

    Bitmap bitmap;
    is = getStreamToUri(context, photoUri);
    if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
        float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
        float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
        float maxRatio = Math.max(widthRatio, heightRatio);

        // Create the bitmap from file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = (int) maxRatio;
        bitmap = BitmapFactory.decodeStream(is, null, options);
    } else {
        bitmap = BitmapFactory.decodeStream(is);
    }
    is.close();

    /*
     * if the orientation is not 0 (or -1, which means we don't know), we
     * have to do a rotation.
     */
    if (orientation > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    String type = "image/png";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (type.equals("image/png")) {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    }
    byte[] bMapArray = baos.toByteArray();
    baos.close();
    return bMapArray;
}

From source file:com.ridgelineapps.wallpaper.Utils.java

public static Bitmap downloadBitmap(String url) {
    try {//from   ww w  .  j  a v  a 2s  . c o m
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        HttpResponse response = client.execute(request);
        InputStream in = response.getEntity().getContent();

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferQualityOverSpeed = true;
        //            options.inDither = true;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        //TODO: use scale like we do loading from file

        Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
        in.close();
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    //        final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    //        final HttpGet getRequest = new HttpGet(url);
    //        try {
    //            HttpResponse response = client.execute(getRequest);
    //            final int statusCode = response.getStatusLine().getStatusCode();
    //            if (statusCode != HttpStatus.SC_OK) {
    //                return null;
    //            }
    //            final HttpEntity entity = response.getEntity();
    //            if (entity != null) {
    //                InputStream inputStream = null;
    //                try {
    //                    inputStream = entity.getContent();
    //                    final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
    //                    return bitmap;
    //                } finally {
    //                    if (inputStream != null) {
    //                        inputStream.close();
    //                    }
    //                    entity.consumeContent();
    //                }
    //            }
    //        } catch (Exception e) {
    //            getRequest.abort();
    //            e.printStackTrace();
    //        } finally {
    //            if (client != null) {
    //                client.close();
    //            }
    //        }
    //        return null;
}

From source file:ir.rasen.charsoo.controller.image_loader.core.decode.BaseImageDecoder.java

protected ImageFileInfo defineImageSizeAndRotation(InputStream imageStream, ImageDecodingInfo decodingInfo)
        throws IOException {
    Options options = new Options();
    options.inJustDecodeBounds = true;/*from   w ww  .j  av a2  s .co  m*/
    BitmapFactory.decodeStream(imageStream, null, options);

    ExifInfo exif;
    String imageUri = decodingInfo.getImageUri();
    if (decodingInfo.shouldConsiderExifParams() && canDefineExifParams(imageUri, options.outMimeType)) {
        exif = defineExifOrientation(imageUri);
    } else {
        exif = new ExifInfo();
    }
    return new ImageFileInfo(new ImageSize(options.outWidth, options.outHeight, exif.rotation), exif);
}

From source file:com.android.xbrowser.DownloadTouchIcon.java

@Override
public Void doInBackground(String... values) {
    if (mContentResolver != null) {
        mCursor = Bookmarks.queryCombinedForUrl(mContentResolver, mOriginalUrl, mUrl);
    }/*from   www  .ja  va  2s .  c om*/

    boolean inDatabase = mCursor != null && mCursor.getCount() > 0;

    String url = values[0];

    if (inDatabase || mMessage != null) {
        AndroidHttpClient client = null;
        HttpGet request = null;

        try {
            client = AndroidHttpClient.newInstance(mUserAgent);
            HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, url);
            if (httpHost != null) {
                ConnRouteParams.setDefaultProxy(client.getParams(), httpHost);
            }

            request = new HttpGet(url);

            // Follow redirects
            HttpClientParams.setRedirecting(client.getParams(), true);

            HttpResponse response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream content = entity.getContent();
                    if (content != null) {
                        Bitmap icon = BitmapFactory.decodeStream(content, null, null);
                        if (inDatabase) {
                            storeIcon(icon);
                        } else if (mMessage != null) {
                            Bundle b = mMessage.getData();
                            b.putParcelable(BrowserContract.Bookmarks.TOUCH_ICON, icon);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            if (request != null) {
                request.abort();
            }
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }

    if (mCursor != null) {
        mCursor.close();
    }

    if (mMessage != null) {
        mMessage.sendToTarget();
    }

    return null;
}

From source file:com.ibuildapp.romanblack.CataloguePlugin.utils.Utils.java

/**
 * Opens Bitmap from stream//w  w w.jav  a 2 s .c om
 *
 * @param stream - input stream
 * @param config decoding config
 * @return bitmap
 */
public static Bitmap proccessBitmap(InputStream stream, Bitmap.Config config) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inPreferredConfig = config;
    Bitmap bitmap = null;
    try {
        // decode image with appropriate options
        try {
            System.gc();
            bitmap = BitmapFactory.decodeStream(stream, null, opts);
        } catch (Exception ex) {
            Log.d("", "");
        } catch (OutOfMemoryError e) {
            Log.d("", "");
            System.gc();
            try {
                bitmap = BitmapFactory.decodeStream(stream, null, opts);
            } catch (Exception ex) {
                Log.d("", "");
            } catch (OutOfMemoryError ex) {
                Log.e("decodeImageFile", "OutOfMemoryError");
            }
        }
    } catch (Exception e) {
        Log.d("", "");
        return null;
    }

    return bitmap;
}

From source file:com.truebanana.bitmap.BitmapUtils.java

public static Bitmap decodeStream(InputStream inputStream, int targetWidth, int targetHeight) {
    try {/*from  ww w  .  j  a  v a 2s .  co m*/
        byte[] bytes = IOUtils.toByteArray(inputStream);
        InputStream is1 = new ByteArrayInputStream(bytes);
        InputStream is2 = new ByteArrayInputStream(bytes);
        if (targetWidth > 1 && targetHeight > 1) {
            BitmapFactory.Options options = getBounds(is1);
            options.inJustDecodeBounds = false;
            options.inSampleSize = getInSampleSize(options, targetWidth, targetHeight);
            return BitmapFactory.decodeStream(is2, new Rect(), options);
        } else {
            return BitmapFactory.decodeStream(is2);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.gmail.altakey.lucene.AsyncImageLoader.java

protected BitmapDrawable doInBackground(Void... args) {
    final Context context = this.view.getContext();
    final Resources res = context.getResources();

    try {/* w  w  w  .ja v a 2s. co m*/
        InputStream in = this.read(new ProgressReportingInputStream.ProgressListener() {
            public void onAdvance(long at, long size) {
                publishProgress(at, size);
            }
        });
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inDither = true;
        bfo.inPreferredConfig = Bitmap.Config.RGB_565;
        try {
            bfo.inPreferQualityOverSpeed = true;
        } catch (NoSuchFieldError e) {
        }
        Bitmap bitmap = BitmapFactory.decodeStream(in, new Rect(-1, -1, -1, -1), bfo);
        return new BitmapDrawable(res, this.scale(bitmap));
    } catch (FileNotFoundException e) {
        return null;
    } catch (OutOfMemoryError e) {
        this.oomMessage.show();
        return null;
    }
}

From source file:mobisocial.musubi.util.UriImage.java

private void decodeBoundsInfo() throws IOException {
    InputStream input = null;//from   w  w  w . j  a  v  a2  s.  com
    try {
        input = openInputStream(mUri);
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(input, null, opt);
        mWidth = opt.outWidth;
        mHeight = opt.outHeight;
    } catch (FileNotFoundException e) {
        // Ignore
        Log.e(TAG, "IOException caught while opening stream", e);
    } finally {
        if (null != input) {
            try {
                input.close();
            } catch (IOException e) {
                // Ignore
                Log.e(TAG, "IOException caught while closing stream", e);
            }
        }
    }
}

From source file:com.neudesic.mobile.pulse.ui.drawable.DrawableManager.java

public Bitmap fetchBitmapImage(String imageUrl) throws FileNotFoundException {
    InputStream is = null;/*  w  w  w  . jav  a  2s . co  m*/
    BufferedInputStream buf = null;

    try {
        is = (InputStream) fetchImage(imageUrl);
        buf = new BufferedInputStream(is);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap bmp = null;
    BitmapFactory.Options ops = new BitmapFactory.Options();
    ops.inJustDecodeBounds = false;
    bmp = BitmapFactory.decodeStream(buf, null, ops);

    return bmp;
}

From source file:com.normalexception.app.rx8club.handler.AvatarLoader.java

/**
 * Decode the image from memory, and reduce the file size
 * @param f   The file//from   w w w  .  jav  a 2 s  .c  o  m
 * @return   The image as a bitmap
 */
private Bitmap decodeFile(File f) {
    try {
        //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 = 70;
        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
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}