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

public static Bitmap decodeSampledBitmapFromFileDescriptor(FileDescriptor fd, long allowedBmpMaxMemorySize) {
    try {//from   w  w w  . j  a  va  2 s  .c o  m
        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;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFileDescriptor(FileDescriptor stream, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* ww  w  . ja v  a2 s.c  o  m*/
    options.inPurgeable = true;
    BitmapFactory.decodeFileDescriptor(stream, null, options);

    if (options.outHeight > 0 && options.outWidth > 0) {
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFileDescriptor(stream, null, options);
    } else {
        return null;
    }
}

From source file:Main.java

public static BitmapDrawable getBitmapDrawableFromUrl(Resources res, URL trueUrl,
        BitmapFactory.Options mOptions) throws Exception {
    Bitmap bitmap = null;/*from   ww w. ja  va2  s  .  com*/
    FileInputStream mFS = null;
    try {
        mFS = new FileInputStream(trueUrl.getPath());
        bitmap = BitmapFactory.decodeFileDescriptor(mFS.getFD(), null, mOptions);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (mFS != null) {
            mFS.close();
        }
    }
    return new BitmapDrawable(res, bitmap);
}

From source file:Main.java

public static Bitmap getTempBitmap(Context context) {
    Bitmap capturedBitmap = null;/*w  w w.  j a  v  a  2 s.co m*/
    final File file = getTempFile(context);
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        AssetFileDescriptor fileDescriptor = context.getContentResolver()
                .openAssetFileDescriptor(Uri.fromFile(file), "r");
        capturedBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return capturedBitmap;
}

From source file:Main.java

public static void setImageViewWidthBitmap(String imgPath, ImageView imageView) throws IOException {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* ww  w  .  ja  v  a 2s .  c  o  m*/
    BitmapFactory.decodeFile(imgPath, options);
    options.inSampleSize = Math.min(options.outWidth / imageView.getWidth(),
            options.outHeight / imageView.getWidth());
    options.inJustDecodeBounds = false;
    options.inPurgeable = true;
    options.inInputShareable = true;

    FileInputStream fis = new FileInputStream(imgPath);
    imageView.setImageBitmap(BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options));
}

From source file:Main.java

/**
 * Reads a Bitmap from an Uri./*from w  ww .j a  v  a  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.getFileDescriptor(), null, options);
            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
 * @param reqWidth       The requested width of the resulting bitmap
 * @param reqHeight      The requested height of the resulting bitmap
 * @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 www.  j a  v  a2 s.com*/
public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, int reqWidth,
        int reqHeight) {

    // 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;

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

From source file:Main.java

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

    Bitmap bitmap;/*ww  w.  j  a v a  2  s .  c  om*/
    AssetFileDescriptor fileDescriptor;
    int reqWidthInPixel = (int) dipToPixels(res, reqWidthInDip);
    int reqHeightInPixel = (int) dipToPixels(res, reqHeightInDip);
    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 getBitmap(String path, int requiredSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from www  .  j  av a2 s  .c o m
    BitmapFactory.decodeFile(path, options);
    int scale = 1;

    while (options.outWidth / scale > requiredSize && options.outHeight / scale > requiredSize)
        scale *= 2;

    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inTempStorage = new byte[32 * 1024];

    Bitmap result = null;
    File file = new File(path);
    FileInputStream fs = null;
    try {
        fs = new FileInputStream(file);

        try {
            result = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
        } catch (OutOfMemoryError oom) {
            oom.printStackTrace();

            try {
                options.inSampleSize *= 4;
                result = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
            } catch (OutOfMemoryError oom1) {
                oom.printStackTrace();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fs != null)
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return result;
}

From source file:Main.java

public 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/*w  ww.  ja v  a 2s .  c om*/
    // used to display this drawable. Take it into account now, so we don't
    // have to
    // scale later.
    w -= 1;
    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;
}