Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

import java.io.FileDescriptor;

public class Main {
    public static Bitmap decodeSampledBitmapFromFileDescriptor(FileDescriptor fd, long allowedBmpMaxMemorySize) {
        try {
            final Options options = new Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fd, null, options);
            options.inSampleSize = calculateInSampleSize(options, allowedBmpMaxMemorySize);
            options.inJustDecodeBounds = false;
            Bitmap ret = BitmapFactory.decodeFileDescriptor(fd, null, options);

            return ret;
        } catch (Error err) {
            err.printStackTrace();

        }
        return null;
    }

    /**
     * assume one pix use 4 bytes.Bitmap.Config.ARGB_8888.
     */
    private final static int calculateInSampleSize(Options options, long reqMemorySize) {
        final int onePixBytes = 4;
        int reqPixs = (int) (reqMemorySize / onePixBytes);
        final int height = options.outHeight;
        final int width = options.outWidth;
        int orgPixs = height * width;
        int inSampleSize = 1;
        while (orgPixs / Math.pow(inSampleSize, 2) > reqPixs) {
            inSampleSize *= 2;

        }
        return inSampleSize;

    }
}