Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.BitmapFactory;

import android.util.DisplayMetrics;
import android.util.Log;
import java.lang.reflect.Field;

public class Main {
    private static final String TAG = "BitmapUtils";

    public static BitmapFactory.Options getBitmapOptions(DisplayMetrics mDisplayMetrics) {
        try {
            // TODO I think this can all be done without reflection now because all these properties are SDK 4
            final Field density = DisplayMetrics.class.getDeclaredField("DENSITY_DEFAULT");
            final Field inDensity = BitmapFactory.Options.class.getDeclaredField("inDensity");
            final Field inTargetDensity = BitmapFactory.Options.class.getDeclaredField("inTargetDensity");
            final Field targetDensity = DisplayMetrics.class.getDeclaredField("densityDpi");
            final BitmapFactory.Options options = new BitmapFactory.Options();
            inDensity.setInt(options, density.getInt(null));
            inTargetDensity.setInt(options, targetDensity.getInt(mDisplayMetrics));
            return options;
        } catch (final IllegalAccessException ex) {
            Log.d(TAG, "Couldn't access fields.", ex);
        } catch (final NoSuchFieldException ex) {
            Log.d(TAG, "Couldn't find fields.", ex);
        }
        return null;
    }
}