Android - retrieve the device-appropriate drawable folder as a string

依然范特西╮ 提交于 2020-02-08 04:39:05

问题


Is it possible to retrieve the correct drawable folder name as a String using Android?

For example, I would like, as a string:

String drawableFolder = someFunction.getFolder();
Log.i("", drawableFolder);

....to output:

XXDPI

.

Usage case: I'm downloading images from a remote URL and would like a mechanism to retrieve a device-appropriate image. It occurred to me I could prepend the URL with the correct name, e.g. http://static.images.server.com/XXDPI_image.png


回答1:


You can get the following through device density

int density= getResources().getDisplayMetrics().densityDpi;

switch(density)
{
case DisplayMetrics.DENSITY_LOW:
    Log.d(TAG, "LDPI");
    break;
case DisplayMetrics.DENSITY_MEDIUM:
     Log.d(TAG, "MDPI");
    break;
case DisplayMetrics.DENSITY_HIGH:
    Log.d(TAG, "HDPI");
    break;
case DisplayMetrics.DENSITY_XHIGH:
     Log.d(TAG, "XHDPI");
    break;
}


来源:https://stackoverflow.com/questions/29769391/android-retrieve-the-device-appropriate-drawable-folder-as-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!