read Shrink Bitmap - Android Graphics

Android examples for Graphics:Bitmap Scale

Description

read Shrink Bitmap

Demo Code


//package com.java2s;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

import android.net.Uri;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static Bitmap readShrink(Context context, Uri uri, int size) {

        InputStream readStream = null;
        InputStream writeStream = null;
        try {/* www  .  j a v a2s.  co  m*/
            readStream = context.getContentResolver().openInputStream(uri);
            Options bmpFactoryOptions = new Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(readStream, null, bmpFactoryOptions);

            int ratio = (int) Math.min(bmpFactoryOptions.outWidth
                    / (float) size, bmpFactoryOptions.outHeight
                    / (float) size);
            bmpFactoryOptions.inSampleSize = ratio;
            bmpFactoryOptions.inJustDecodeBounds = false;

            writeStream = context.getContentResolver().openInputStream(uri);
            Bitmap bitmap = BitmapFactory.decodeStream(writeStream, null,
                    bmpFactoryOptions);
            return bitmap;
        } catch (FileNotFoundException e) {
        } finally {
            try {
                readStream.close();
            } catch (IOException e) {
            }
            try {
                writeStream.close();
            } catch (IOException e) {
            }
        }
        return null;
    }
}

Related Tutorials