Example usage for android.graphics BitmapFactory decodeFileDescriptor

List of usage examples for android.graphics BitmapFactory decodeFileDescriptor

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeFileDescriptor.

Prototype

public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts) 

Source Link

Document

Decode a bitmap from the file descriptor.

Usage

From source file:br.com.viniciuscr.notification2android.mediaPlayer.MusicUtils.java

private static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) {
    // NOTE: There is in fact a 1 pixel border on the right side in the ImageView
    // used to display this drawable. Take it into account now, so we don't have to
    // scale later.
    w -= 1;/*ww w.j a v a 2  s.com*/
    ContentResolver res = context.getContentResolver();
    Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
    if (uri != null) {
        ParcelFileDescriptor fd = null;
        try {
            fd = res.openFileDescriptor(uri, "r");
            int sampleSize = 1;

            // Compute the closest power-of-two scale factor
            // and pass that to sBitmapOptionsCache.inSampleSize, which will
            // result in faster decoding and better quality
            sBitmapOptionsCache.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);
            int nextWidth = sBitmapOptionsCache.outWidth >> 1;
            int nextHeight = sBitmapOptionsCache.outHeight >> 1;
            while (nextWidth > w && nextHeight > h) {
                sampleSize <<= 1;
                nextWidth >>= 1;
                nextHeight >>= 1;
            }

            sBitmapOptionsCache.inSampleSize = sampleSize;
            sBitmapOptionsCache.inJustDecodeBounds = false;
            Bitmap b = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);

            if (b != null) {
                // finally rescale to exactly the size we need
                if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
                    Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
                    // Bitmap.createScaledBitmap() can return the same bitmap
                    if (tmp != b)
                        b.recycle();
                    b = tmp;
                }
            }

            return b;
        } catch (FileNotFoundException e) {
        } finally {
            try {
                if (fd != null)
                    fd.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}

From source file:us.theparamountgroup.android.inventory.EditorActivity.java

private Bitmap getBitmapFromUri(Uri uri) {
    if (uri == null) {
        return null;
    }//  ww w  .j a v  a  2s. com
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();
    ParcelFileDescriptor parcelFileDescriptor = null;
    try {
        parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts);
        int photoW = opts.outWidth;
        int photoH = opts.outHeight;

        int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

        opts.inJustDecodeBounds = false;
        opts.inSampleSize = scaleFactor;
        opts.inPurgeable = true;
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts);
        return image;

    } catch (Exception e) {
        Log.e(LOG_TAG, "Failed to load image.", e);
        return null;
    } finally {
        try {
            if (parcelFileDescriptor != null) {
                parcelFileDescriptor.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(LOG_TAG, "Error closing ParcelFile Descriptor");
        }
    }
}

From source file:com.inter.trade.ui.fragment.agent.AgentApplyFragmentNew.java

public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, int reqWidth,
        int reqHeight, ImageCache cache) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*  w  w w.j av  a 2  s .  co  m*/
    BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqWidth);
    //
    //        // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    options.inScaled = false;
    //
    //        options.inInputShareable = true;
    //        options.inPurgeable = true;
    //        
    // If we're running on Honeycomb or newer, try to use inBitmap

    return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
}