Android Bitmap Load loadBitmap(Context c, String fileName, int width, boolean sample)

Here you can find the source of loadBitmap(Context c, String fileName, int width, boolean sample)

Description

load Bitmap

Declaration

public static Bitmap loadBitmap(Context c, String fileName, int width,
            boolean sample) 

Method Source Code

//package com.java2s;
import java.io.File;

import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

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

public class Main {
    private static int count = 0;

    public static Bitmap loadBitmap(InputStream is) {
        try {//from   w  w  w .  ja  v  a  2  s . c  o m
            if (is != null && is.available() > 0) {
                return BitmapFactory.decodeStream(is);
            } else {
                return null;
            }
        } catch (IOException e) {
            return null;
        } catch (OutOfMemoryError e) {
            return null;
        } finally {

        }
    }

    public static Bitmap loadBitmap(Context c, String fileName) {
        ParcelFileDescriptor pfd;
        try {
            pfd = c.getContentResolver().openFileDescriptor(
                    Uri.parse("file://" + fileName), "r");
        } catch (IOException ex) {
            return null;
        }
        java.io.FileDescriptor fd = pfd.getFileDescriptor();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fd, null, options);
        options.inSampleSize = computeSampleSize(options, 400);
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        Bitmap sourceBitmap = BitmapFactory.decodeFileDescriptor(fd, null,
                options);
        if (count < 4) {
            count++;
        } else {
            count = 0;
            System.gc();
        }
        return sourceBitmap;
    }

    public static Bitmap loadBitmap(Context c, String fileName, int width) {
        ParcelFileDescriptor pfd;
        try {
            pfd = c.getContentResolver().openFileDescriptor(
                    Uri.parse("file://" + fileName), "r");
        } catch (IOException ex) {
            return null;
        }
        java.io.FileDescriptor fd = pfd.getFileDescriptor();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fd, null, options);
        options.inSampleSize = computeSampleSize(options, width);
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        try {
            Bitmap sourceBitmap = BitmapFactory.decodeFileDescriptor(fd,
                    null, options);
            if (count < 4) {
                count++;
            } else {
                count = 0;
                System.gc();
            }
            return sourceBitmap;
        } catch (OutOfMemoryError e) {

            return loadBitmap(c, fileName, (int) (width * 0.5));
        }

    }

    public static Bitmap loadBitmap(Context c, String fileName, int width,
            boolean sample) {
        ParcelFileDescriptor pfd;
        try {
            pfd = c.getContentResolver().openFileDescriptor(
                    Uri.parse("file://" + fileName), "r");
        } catch (IOException ex) {
            return null;
        }
        java.io.FileDescriptor fd = pfd.getFileDescriptor();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fd, null, options);
        if (sample) {
            options.inSampleSize = computeSampleSize(options, width);
        } else {
            File file = new File(fileName);
            if (!file.exists()) {
                return null;
            }
            if (file.length() > 256 * 1024) {
                int presample = computeSampleSize(options, width);
                if (presample > 2 && file.length() / (2 * 2) < 256 * 1024) {
                    options.inSampleSize = 2;
                } else if (presample > 3
                        && file.length() / (3 * 3) < 256 * 1024) {
                    options.inSampleSize = 3;
                } else if (presample > 4
                        && file.length() / (4 * 4) < 256 * 1024) {
                    options.inSampleSize = 4;
                } else if (file.length() / (presample * presample) > 256 * 1024) {
                    options.inSampleSize = presample + 1;
                } else {
                    options.inSampleSize = presample;
                }

            } else {
                options.inSampleSize = 1;
            }
        }

        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        Bitmap sourceBitmap = BitmapFactory.decodeFileDescriptor(fd, null,
                options);
        if (count < 4) {
            count++;
        } else {
            count = 0;
            System.gc();
        }
        return sourceBitmap;
    }

    public static int computeSampleSize(BitmapFactory.Options options,
            int target) {
        int w = options.outWidth;
        int h = options.outHeight;
        float candidateW = w / target;
        float candidateH = h / target;
        float candidate = Math.max(candidateW, candidateH);
        candidate = (float) (candidate + 0.0);
        if (candidate <= 1.0)
            return 1;
        return (int) candidate;
    }
}

Related

  1. getImageFromUri(Context ctx, Uri uri, int reqWidth, int reqHeight)
  2. getNetBitmap(String strUrl, File file, Context context, File file2)
  3. loadBitmap(Activity activity, int resId, ImageView imageView, int reqWidth, int reqHeight)
  4. loadBitmap(Context c, String fileName)
  5. loadBitmap(Context c, String fileName, int width)
  6. loadBitmap(InputStream is)
  7. loadBitmap(String bitmapUrl, ImageView imageView, int reqWidth, int reqHeight)
  8. loadBitmap(String url)
  9. loadBitmap(String url)