Question

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;
Was it helpful?

Solution

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);

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top