Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.lang.ref.SoftReference;

import java.util.WeakHashMap;

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;

public class Main {
    private static WeakHashMap<String, SoftReference<BitmapDrawable>> mCache = new WeakHashMap<String, SoftReference<BitmapDrawable>>();

    public static BitmapDrawable getBitmapDrawable(Context context, Uri uri) {
        System.out.println("uri path = " + uri.getPath());
        Bitmap b;
        try {
            String path = uri.getPath();
            if (mCache.containsKey(path)) {
                BitmapDrawable d = mCache.get(path).get();
                if (d != null) {
                    System.out.println("not recycle path = " + uri.getPath());
                    return d;
                } else {
                    System.out.println("be recycle path = " + uri.getPath());
                    mCache.remove(path);
                }
            }
            System.out.println("----->safeDecodeStream start");
            b = safeDecodeStream(context, uri, 60, 60);
            System.out.println("----->safeDecodeStream end");
            final BitmapDrawable bd = new BitmapDrawable(b);
            mCache.put(path, new SoftReference<BitmapDrawable>(bd));
            return bd;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * A safer decodeStream method
     * rather than the one of {@link BitmapFactory} which will be easy to get OutOfMemory Exception
     * while loading a big image file.
     * 
     * @param uri
     * @param width
     * @param height
     * @return
     * @throws FileNotFoundException
     */
    protected static Bitmap safeDecodeStream(Context context, Uri uri, int width, int height)
            throws FileNotFoundException {
        int scale = 1;
        // Decode image size without loading all data into memory  
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        android.content.ContentResolver resolver = context.getContentResolver();
        try {

            BitmapFactory.decodeStream(new BufferedInputStream(resolver.openInputStream(uri), 4 * 1024), null,
                    options);
            if (width > 0 || height > 0) {
                options.inJustDecodeBounds = true;
                int w = options.outWidth;
                int h = options.outHeight;
                while (true) {
                    if ((width > 0 && w / 2 < width) || (height > 0 && h / 2 < height)) {
                        break;
                    }
                    w /= 2;
                    h /= 2;
                    scale *= 2;
                }
            }
            // Decode with inSampleSize option  
            options.inJustDecodeBounds = false;
            options.inSampleSize = scale;
            return BitmapFactory.decodeStream(new BufferedInputStream(resolver.openInputStream(uri), 4 * 1024),
                    null, options);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            System.gc();
        }
        return null;
    }
}