0

One of my users get a pixel per inch value of 0.288. ( getPixelsperInch=0.288 )

I am using the following code to determine the pixelperinche. What am I doing wrong here ?

His Screensize is 976/600. With my own devices I get reasonable values. The problematic device is a "UTOK700Q".

public float getPixelsperInch() {

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    float fpixelperinch = dm.xdpi;

    return fpixelperinch;
}




    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int iScreenHeight = metrics.heightPixels;
    int iScreenWidth = metrics.widthPixels;
mcfly soft
  • 11,289
  • 26
  • 98
  • 202

2 Answers2

1

Do like this....

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);


// density interms of dpi
Log.i(TAG, "D density :" +  metrics.densityDpi);
Cheerag
  • 415
  • 3
  • 10
  • Thank you. This works. I will accept the answer soon. I still have a problem with it . Perhabs you have a hint. I am needing the density in X and Y direction to calculate the diagonal inches of a device like described in http://stackoverflow.com/questions/15055458/detect-7-inch-and-10-inch-tablet-programmatically. For that I need the X and Y Density. Any hint ? – mcfly soft May 13 '14 at 14:03
  • float xDpi = metrics.xdpi; float yDpi = metrics.ydpi; – Cheerag May 14 '14 at 10:10
  • metrics.xdpi gives me 0.288 pixels per inch, which cannot be. Your answer with densityDpi gives me a reasonable value, but I am not sure if it is correct for X and Y. To evaluate the diagonal inches I need X and Y. But your answer is correct regarding the question. – mcfly soft May 14 '14 at 11:51
0

May this will help you...

WindowManager windowManager = getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);

    // since SDK_INT = 1;
    mWidthPixels = displayMetrics.widthPixels;
    mHeightPixels = displayMetrics.heightPixels;

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
        try {
            mWidthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
            mHeightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
        } catch (Exception ignored) {
        }
    }

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17) {
        try {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
            mWidthPixels = realSize.x;
            mHeightPixels = realSize.y;
        } catch (Exception ignored) {
        }
    }
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(mWidthPixels / dm.xdpi, 2);
    double y = Math.pow(mHeightPixels / dm.ydpi, 2);
    double screenInches = Math.sqrt(x + y);
    Log.d("debug", "Screen inches : " + screenInches);
Cheerag
  • 415
  • 3
  • 10