Make a bitmap from a given Uri. - Android Graphics

Android examples for Graphics:Bitmap Create

Description

Make a bitmap from a given Uri.

Demo Code

/*/* w  ww  .j a v  a  2  s.  com*/
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
import java.io.FileDescriptor;
import java.io.IOException;

import android.content.ContentResolver;

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

import android.net.Uri;
import android.os.ParcelFileDescriptor;

import android.util.Log;

public class Main {
    private static final String TAG = "ThumbnailUtil";
    public static final boolean NO_NATIVE = false;
    public static final int UNCONSTRAINED = -1;

    /**
     * Make a bitmap from a given Uri.
     *
     * @param uri
     */
    public static Bitmap makeBitmap(int minSideLength, int maxNumOfPixels,
            Uri uri, ContentResolver cr) {
        return makeBitmap(minSideLength, maxNumOfPixels, uri, cr, NO_NATIVE);
    }

    public static Bitmap makeBitmap(int minSideLength, int maxNumOfPixels,
            Uri uri, ContentResolver cr, boolean useNative) {
        ParcelFileDescriptor input = null;
        try {
            input = cr.openFileDescriptor(uri, "r");
            BitmapFactory.Options options = null;
            if (useNative) {
                options = createNativeAllocOptions();
            }
            return makeBitmap(minSideLength, maxNumOfPixels, uri, cr,
                    input, options);
        } catch (final IOException ex) {
            Log.e(TAG, "", ex);
            return null;
        } finally {
            closeSilently(input);
        }
    }

    public static Bitmap makeBitmap(int minSideLength, int maxNumOfPixels,
            Uri uri, ContentResolver cr, ParcelFileDescriptor pfd,
            BitmapFactory.Options options) {
        Bitmap b = null;
        try {
            if (pfd == null) {
                pfd = makeInputStream(uri, cr);
            }
            if (pfd == null) {
                return null;
            }
            if (options == null) {
                options = new BitmapFactory.Options();
            }

            final FileDescriptor fd = pfd.getFileDescriptor();
            options.inSampleSize = 1;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fd, null, options);
            if (options.mCancel || options.outWidth == -1
                    || options.outHeight == -1) {
                return null;
            }
            options.inSampleSize = computeSampleSize(options,
                    minSideLength, maxNumOfPixels);
            options.inJustDecodeBounds = false;

            options.inDither = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            b = BitmapFactory.decodeFileDescriptor(fd, null, options);
        } catch (final OutOfMemoryError ex) {
            Log.e(TAG, "Got oom exception ", ex);
            return null;
        } finally {
            closeSilently(pfd);
        }
        return b;
    }

    public static BitmapFactory.Options createNativeAllocOptions() {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inNativeAlloc = true;

        return options;
    }

    private static void closeSilently(ParcelFileDescriptor c) {
        if (c == null) {
            return;
        }
        try {
            c.close();
        } catch (final Throwable t) {
            // do nothing
        }
    }

    private static ParcelFileDescriptor makeInputStream(Uri uri,
            ContentResolver cr) {
        try {
            return cr.openFileDescriptor(uri, "r");
        } catch (final IOException ex) {
            return null;
        }
    }

    public static int computeSampleSize(BitmapFactory.Options options,
            int minSideLength, int maxNumOfPixels) {
        final int initialSize = computeInitialSampleSize(options,
                minSideLength, maxNumOfPixels);

        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

    private static int computeInitialSampleSize(
            BitmapFactory.Options options, int minSideLength,
            int maxNumOfPixels) {
        final double w = options.outWidth;
        final double h = options.outHeight;

        final int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1
                : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        final int upperBound = (minSideLength == UNCONSTRAINED) ? 128
                : (int) Math.min(Math.floor(w / minSideLength),
                        Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }

        if ((maxNumOfPixels == UNCONSTRAINED)
                && (minSideLength == UNCONSTRAINED)) {
            return 1;
        } else if (minSideLength == UNCONSTRAINED) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
}

Related Tutorials