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 final Bitmap scaleBitmap(int scaleFactor, InputStream is) {
    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;

    Bitmap bitmap = BitmapFactory.decodeStream(is, null, bmOptions);
    return bitmap;
}

From source file:com.pickr.utils.BitmapUtils.java

private static Bitmap loadBitmap(URL url) throws IOException {
    InputStream is = null;//from www.  j  a v  a2 s . c  o m
    try {
        is = new BufferedInputStream(url.openStream(), 512000);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;

        return BitmapFactory.decodeStream(is, null, options);

    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:Main.java

public static Bitmap getBitmap(Context context, int resId) {
    try {/*  w w w.  j  a v a 2  s .  c  om*/
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = true;
        InputStream is = context.getResources().openRawResource(resId);
        Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(), options);
        is.close();
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:org.kegbot.app.util.Downloader.java

/**
 * Downloads and returns a URL as a {@link Bitmap}.
 *
 * @param url the image to download//from   w  ww  . ja v  a 2 s .  c  om
 * @return a new {@link Bitmap}, or {@code null} if any error occurred
 */
public static Bitmap downloadBitmap(String url) {
    final HttpClient client = new DefaultHttpClient();
    final HttpGet getRequest = new HttpGet(url);

    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w(LOG_TAG, "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;

                inputStream = entity.getContent();
                return BitmapFactory.decodeStream(inputStream, null, options);
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}

From source file:Main.java

static BitmapFactory.Options decodeBounds(InputStream in) throws IOException {
    BitmapFactory.Options options = new BitmapFactory.Options();
    try {// w  w w.  j  a va2 s .  c  o  m
        // Decode image size
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        return options;
    } finally {
        in.close();
    }
}

From source file:fr.gcastel.freeboxV6GeekIncDownloader.services.GeekIncLogoDownloadService.java

private void saveAndResizeFile(File tmpFile, File outFile, int requiredSize)
        throws FileNotFoundException, IOException {
    FileInputStream fis = new FileInputStream(tmpFile);
    BufferedInputStream bfis = new BufferedInputStream(fis);

    // Recherche de la taille sans charger entirement l'image
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;//from  ww  w  .  jav a 2s . c  o  m
    BitmapFactory.decodeStream(bfis, null, o);

    // Calcule la bonne mise  l'chelle en cherchant la puissance de 2 la
    // plus proche
    int scale = 1;
    while (o.outWidth / scale / 2 >= requiredSize && o.outHeight / scale / 2 >= requiredSize)
        scale *= 2;

    // Mise  l'chelle !
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;

    bfis.close();
    fis.close();

    fis = new FileInputStream(tmpFile);
    bfis = new BufferedInputStream(fis);
    Bitmap bmp = BitmapFactory.decodeStream(bfis, null, o2);
    bfis.close();

    FileOutputStream fout = new FileOutputStream(outFile);
    BufferedOutputStream bout = new BufferedOutputStream(fout);
    bmp.compress(Bitmap.CompressFormat.PNG, 90, bout);
    bout.close();
    fout.close();
}

From source file:Main.java

public static Bitmap scaleBitmap(int scaleFactor, InputStream is) {
    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;

    return BitmapFactory.decodeStream(is, null, bmOptions);
}

From source file:Main.java

public static Bitmap load(InputStream inputStream, int inSampleSize) {
    try {//from   ww  w. j a  v  a2  s . com
        Options opts = new Options();
        opts.inSampleSize = inSampleSize;
        opts.inDither = false;
        opts.inPreferredConfig = Config.ARGB_8888;

        Bitmap result = BitmapFactory.decodeStream(inputStream, null, opts);
        return result == null ? EMPTY_BITMAP : result;
    } catch (OutOfMemoryError e) {
        Log.e("LoadImage", e.getMessage(), e);
    }
    return EMPTY_BITMAP;
}

From source file:com.example.util.ImageUtils.java

public static Bitmap getImageFromUrl(String url) {

    //        SessionManager cache = SessionManager.getInstance();

    HttpGet httpRequest = new HttpGet(url);
    HttpResponse response = null;//from  ww  w . j a va  2s.  c  om
    AndroidHttpClient client = HttpClientFactory.get().getHttpClient();
    try {
        response = client.execute(httpRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                // non-compressed images no need to be cached to the sd-card
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inPurgeable = true;
                Bitmap bmp = BitmapFactory.decodeStream(new FlushedInputStream(inputStream), null, o);
                //                    cache.saveToDisk(url, bmp);
                return bmp;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (ClientProtocolException e) {
        Log.w("ImageDownloader", "ClientProtocolException " + url);
    } catch (IOException e) {
        Log.w("ImageDownloader", "IOException " + url);
    } catch (Exception e) {
        Log.w("ImageDownloader", "other exception when download images from " + url);
    } catch (OutOfMemoryError err) {
        Log.w("ImageDownloader", "OutOfMemoryError when download images from " + url);
    } finally {
        httpRequest.abort();
    }
    return null;
}

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

public static BitmapFactory.Options getBounds(InputStream inputStream, BitmapFactory.Options options) {
    options.inJustDecodeBounds = true;/*from w ww  .  j a v  a2 s  .co m*/
    BitmapFactory.decodeStream(inputStream, new Rect(), options);
    return options;
}