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:Main.java

/**
 * Reads a Bitmap from an Uri.//from w w w .ja  va  2s  . c  o  m
 *
 * @param context
 * @param selectedImage
 * @return Bitmap
 */
public static Bitmap readBitmap(Context context, Uri selectedImage) {
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inScaled = false;
    //      options.inSampleSize = 3;
    AssetFileDescriptor fileDescriptor = null;
    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        try {
            bm = BitmapFactory.decodeFileDescriptor(
                    fileDescriptor != null ? fileDescriptor.getFileDescriptor() : null, null, options);
            if (fileDescriptor != null) {
                fileDescriptor.close();
            }
        } catch (IOException e) {
            return null;
        }
    }
    return bm;
}

From source file:Main.java

/**
 * Decode and sample down a bitmap from a file input stream to the requested width and height.
 *
 * @param fileDescriptor The file descriptor to read from
 * @return A bitmap sampled down from the original with the same aspect ratio and dimensions
 *         that are equal to or greater than the requested width and height
 *///from ww w . j a  v a  2  s  .  c o  m
public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

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

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(Resources res, ContentResolver contentResolver, File file,
        int reqWidthInPixel, int reqHeightInPixel) throws IOException {

    Bitmap bitmap;/*from  ww  w. jav  a  2s  . co  m*/
    AssetFileDescriptor fileDescriptor;

    fileDescriptor = contentResolver.openAssetFileDescriptor(Uri.fromFile(file), "r");

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidthInPixel, reqHeightInPixel);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
    fileDescriptor.close();
    return bitmap;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(Resources res, ContentResolver contentResolver, Uri uri,
        float reqWidthInDip, float reqHeightInDip) throws IOException {

    Bitmap bitmap;/*from   w  w w  .  j a  v a 2s. c  o  m*/
    AssetFileDescriptor fileDescriptor;
    int reqWidthInPixel = (int) dipToPixels(res, reqWidthInDip);
    int reqHeightInPixel = (int) dipToPixels(res, reqHeightInDip);
    fileDescriptor = contentResolver.openAssetFileDescriptor(uri, "r");

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidthInPixel, reqHeightInPixel);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
    fileDescriptor.close();
    return bitmap;
}

From source file:org.anoopam.main.anoopamaudio.UtilFunctions.java

/**
 * Get the album image from albumId//w  w w .j  a v a  2 s  . com
 * @param context
 * @param album_id
 * @return
 */
public static Bitmap getAlbumart(Context context, Long album_id) {
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    try {
        final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
        Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
        ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
        if (pfd != null) {
            FileDescriptor fd = pfd.getFileDescriptor();
            bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
            pfd = null;
            fd = null;
        }
    } catch (Error ee) {
    } catch (Exception e) {
    }
    return bm;
}

From source file:Main.java

/**
 * This method first examines if the thumbnail embedded in EXIF is bigger than our target
 * size. If not, then it'll create a thumbnail from original image. Due to efficiency
 * consideration, we want to let MediaThumbRequest avoid calling this method twice for
 * both kinds, so it only requests for MICRO_KIND and set saveImage to true.
 *
 * This method always returns a "square thumbnail" for MICRO_KIND thumbnail.
 *
 * @param filePath the path of image file
 * @param kind could be MINI_KIND or MICRO_KIND
 * @return Bitmap//  w  w w  .  j a va2 s.c  om
 *
 * @hide This method is only used by media framework and media provider internally.
 */
public static Bitmap createImageThumbnail(String filePath, int kind) {
    boolean wantMini = (kind == Images.Thumbnails.MINI_KIND);
    int targetSize = wantMini ? TARGET_SIZE_MINI_THUMBNAIL : TARGET_SIZE_MICRO_THUMBNAIL;
    int maxPixels = wantMini ? MAX_NUM_PIXELS_THUMBNAIL : MAX_NUM_PIXELS_MICRO_THUMBNAIL;
    //        SizedThumbnailBitmap sizedThumbnailBitmap = new SizedThumbnailBitmap();
    Bitmap bitmap = null;
    //        MediaFileType fileType = ImageFile.getFileType(filePath);
    //        if (fileType != null && fileType.fileType == ImageFile.FILE_TYPE_JPEG) 
    //        {
    //            createThumbnailFromEXIF(filePath, targetSize, maxPixels, sizedThumbnailBitmap);
    //            bitmap = sizedThumbnailBitmap.mBitmap;
    //        }

    if (bitmap == null) {
        try {
            FileDescriptor fd = new FileInputStream(filePath).getFD();
            BitmapFactory.Options options = new BitmapFactory.Options();
            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, targetSize, maxPixels);
            options.inJustDecodeBounds = false;

            options.inDither = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bitmap = BitmapFactory.decodeFileDescriptor(fd, null, options);
        } catch (IOException ex) {
            Log.e(TAG, ex.getMessage());
        }
    }

    if (kind == Images.Thumbnails.MICRO_KIND) {
        // now we make it a "square thumbnail" for MICRO_KIND thumbnail
        bitmap = extractThumbnail(bitmap, TARGET_SIZE_MICRO_THUMBNAIL, TARGET_SIZE_MICRO_THUMBNAIL,
                OPTIONS_RECYCLE_INPUT);
    }
    return bitmap;
}

From source file:Main.java

private static Bitmap getMutableBitmap(FileDescriptor fd, Rect outPadding) {
    return BitmapFactory.decodeFileDescriptor(fd, outPadding, getMutableOption());
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(Resources res, ContentResolver contentResolver, Uri uri,
        int reqWidthInPixel, int reqHeightInPixel) throws IOException {

    Bitmap bitmap;/* w w w  .  j  a  v a  2 s.  c  o m*/
    AssetFileDescriptor fileDescriptor;

    fileDescriptor = contentResolver.openAssetFileDescriptor(uri, "r");

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidthInPixel, reqHeightInPixel);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
    fileDescriptor.close();
    return bitmap;
}

From source file:org.y20k.transistor.helpers.ImageHelper.java

private Bitmap decodeSampledBitmapFromUri(Uri imageUri, int reqWidth, int reqHeight) {

    Bitmap bitmap;//from ww  w  .  j av a  2  s .  c  o m
    ParcelFileDescriptor parcelFileDescriptor = null;

    try {
        parcelFileDescriptor = mActivity.getContentResolver().openFileDescriptor(imageUri, "r");
    } catch (FileNotFoundException e) {
        // TODO handle error
        e.printStackTrace();
    }

    if (parcelFileDescriptor != null) {
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

        // decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

        // calculate inSampleSize
        options.inSampleSize = calculateSampleParameter(options, reqWidth, reqHeight);

        // decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

        return bitmap;

    } else {
        return null;
    }

}

From source file:com.jcsoluciones.superdt.utilities.ImageResizer.java

/**
 * Decode and sample down a bitmap from a file input stream to the requested width and height.
 *
 * @param fileDescriptor The file descriptor to read from
 * @param reqWidth The requested width of the resulting bitmap
 * @param reqHeight The requested height of the resulting bitmap
 * @param cache The ImageCache used to find candidate bitmaps for use with inBitmap
 * @return A bitmap sampled down from the original with the same aspect ratio and dimensions
 *         that are equal to or greater than the requested width and height
 *//* w  w w  . jav  a  2  s  .  co m*/
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;
    BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    // If we're running on Honeycomb or newer, try to use inBitmap
    if (Utils.hasHoneycomb()) {
        addInBitmapOptions(options, cache);
    }

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