Android Bitmap Load decodeFromDescriptor( FileDescriptor descriptor)

Here you can find the source of decodeFromDescriptor( FileDescriptor descriptor)

Description

decode From Descriptor

Declaration

public static final Bitmap decodeFromDescriptor(
            FileDescriptor descriptor) 

Method Source Code

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.media.ThumbnailUtils;
import android.util.Base64;
import android.view.View;

public class Main{
    public static final Bitmap decodeFromDescriptor(
            FileDescriptor descriptor) {
        return decodeFromDescriptor(descriptor, 0, 0);
    }//from  w w  w .  j av  a  2s. c om
    public static final Bitmap decodeFromDescriptor(
            FileDescriptor descriptor, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(descriptor, null, options);
        options.inSampleSize = inSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        Bitmap bitmap;
        try {
            bitmap = BitmapFactory.decodeFileDescriptor(descriptor, null,
                    options);
        } catch (OutOfMemoryError error) {
            options.inSampleSize = options.inSampleSize * 2;
            bitmap = BitmapFactory.decodeFileDescriptor(descriptor, null,
                    options);
        }
        return bitmap;
    }
    public static final int inSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height
                        / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }

            final float totalPixels = width * height;
            final float totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
                inSampleSize++;
            }
        }
        return inSampleSize;
    }
}

Related

  1. loadImage(String urlStr)
  2. loadPreviewBitmap(String fileName, int width, int height)
  3. SafeDecodeBitmapFile( String strFilePath)
  4. decodeF(File f)
  5. decodeFile(File f, int size)
  6. decodeFromDescriptor( FileDescriptor descriptor, int reqWidth, int reqHeight)
  7. getBitmap(Context c, String uri, int minsizeDP)
  8. getImageFromAssetFile(Context context, String fileName)
  9. downloadImageToSd(String urlSource, String pathDest)