0

I am developing a phonegap (cordova) project and I want to get the DPI of the device. Is this possible?

In Android native JAVA API there is a Class named DisplayMetrics which provides getDisplayMetrics().xdpi/ydpi method to get device DPI.

Is there any method to get device DPI in Cordova or Phonegap?

Opal
  • 81,889
  • 28
  • 189
  • 210

1 Answers1

-1

Theres no phonegap feature to get this, however you can do this:

<div id='testdiv' style='height: 1in; left: -100%; position: absolute; top: -100%; width: 1in;'></div>
<script type='text/javascript'>
var devicePixelRatio = window.devicePixelRatio || 1;
dpi_x = document.getElementById('testdiv').offsetWidth * devicePixelRatio;
dpi_y = document.getElementById('testdiv').offsetHeight * devicePixelRatio;
</script>
Lukesmith
  • 170
  • 2
  • 16
  • @Tawsif Patel, you should also read the classic essay by PPK, [A pixel is not a pixel is not a pixel](http://www.quirksmode.org/blog/archives/2010/04/a_pixel_is_not.html) –  May 31 '15 at 07:45
  • 1
    Thanks for the reply, I tried your solution but it does not give perfect DPI of the devices, I have tested on many devices like nexus 7, IPad 3, Samsung Galaxy Note 2, but I dont get exact DPI as mentioned in their device Specification – Tawsif Patel Jun 01 '15 at 04:44
  • This doesn't work. What you're getting here is "CSS pixel density" not the actual density of the device screen. – trusktr Oct 28 '15 at 23:47