image File Path To Bitmap - Android Media

Android examples for Media:Picture

Description

image File Path To Bitmap

Demo Code


//package com.book2s;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.util.Log;

public class Main {
    public static final String TAG = "BlobUtil";

    public static Bitmap imageFilePathToBitmap(Context c, String path,
            int maxDim) {
        Bitmap bmp = null;//  w w w .ja  va2 s.  c  om
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        try {
            // compute the smallest size bitmap we need to read
            FileInputStream fis = new FileInputStream(path);
            BitmapFactory.decodeStream(fis, null, opts);
            try {
                fis.close();
            } catch (IOException e) {
                Log.w(TAG, e.getMessage());
            }

            int w = opts.outWidth;
            int h = opts.outHeight;
            int s = 1;
            while (true) {
                if ((w / 2 < maxDim) || (h / 2 < maxDim)) {
                    break;
                }
                w /= 2;
                h /= 2;
                s *= 2;
            }

            // scale and read the data
            opts.inJustDecodeBounds = false;
            opts.inSampleSize = s;

            fis = new FileInputStream(path);
            bmp = BitmapFactory.decodeStream(fis, null, opts);
            try {
                fis.close();
            } catch (IOException e) {
                Log.w(TAG, e.getMessage());
            }
        } catch (FileNotFoundException e) {
            Log.w(TAG, e.getMessage());
        }
        return bmp;
    }
}

Related Tutorials