Reputation: 2783
Short problem
DisplayMetrics.densityDpi seems to be zero on rooted devices.
Long problem
I am using the following code in my app:
public static int getIconSize(Activity activity) {
int size;
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch (metrics.densityDpi) {
case DisplayMetrics.DENSITY_LOW:
size = 36;
break;
case DisplayMetrics.DENSITY_MEDIUM:
size = 48;
break;
case DisplayMetrics.DENSITY_TV:
size = 64;
break;
case DisplayMetrics.DENSITY_HIGH:
size = 72;
break;
case DisplayMetrics.DENSITY_XHIGH:
size = 96;
break;
case DisplayMetrics.DENSITY_400:
size = 120;
break;
case DisplayMetrics.DENSITY_XXHIGH:
size = 144;
break;
case DisplayMetrics.DENSITY_560:
size = 168;
break;
case DisplayMetrics.DENSITY_XXXHIGH:
size = 192;
break;
default:
size = (int) (metrics.densityDpi * (3/10));
}
return size;
}
I use this to scale icons being made by my app during runtime and scale icons I get from other apps, since they aren't always the right size.
This works great, but on (some) rooted devices it seems to fail. Size is returned as zero, so from the code, we can conclude that metrics.densityDpi must be equal to zero. (I don't have a rooted phone to test this)
I have looked into the <supports-screens>
section of the manifest, but it seems like the default values are the ones I need.
Does anyone know the reason why this is happening and possibly a fix?
Thank you
Upvotes: 1
Views: 1427
Reputation: 2783
Seems like rooted phones don't always return a value for densityDpi. I solved this by adding this at the end op the code in my question. density seems to be returned right.
if (size == 0) {
size = (int) (metrics.density * 48);
}
Upvotes: 2