decode Stream to Bitmap - Android Graphics

Android examples for Graphics:Bitmap Stream

Description

decode Stream to Bitmap

Demo Code


//package com.java2s;
import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static Bitmap decodeStream(final InputStream is) {
        return decodeStream(is, 1, 0, 2);
    }//w w w  . j  a  va 2  s  .  c o m

    public static Bitmap decodeStream(final InputStream is,
            final int startInSampleSize, final int add, final int multi) {
        final BitmapFactory.Options opts = new BitmapFactory.Options();
        int inSampleSize = startInSampleSize;
        while (true) {
            opts.inSampleSize = inSampleSize;
            opts.inDither = true;
            try {
                return BitmapFactory.decodeStream(is, null, opts);
            } catch (final OutOfMemoryError e) {
                inSampleSize = (inSampleSize + add) * multi;
            }
        }
    }

    public static Bitmap decodeStream(final Context context,
            final String name, final BitmapFactory.Options opts)
            throws FileNotFoundException {
        final InputStream in = new BufferedInputStream(
                context.openFileInput(name));
        try {
            return BitmapFactory.decodeStream(in, null, opts);
        } finally {
            try {
                in.close();
            } catch (final IOException e) {
            } // 
        }
    }
}

Related Tutorials