How to identify resource file in use?

折月煮酒 提交于 2019-12-23 04:19:11

问题


Have a number of image files resources e.g. image01.png, image02.png....

I have have set up resource folders with different locale/resolution/orientation qualifiers e.g.

/res/drawable-en-mdpi-port
/res/drawable-en-mdpi-land
/res/drawable-fr-hdpi-port...........

Image files in each differ (aspect ratio/resolution/locale-specfic image)

Is there anyway to programatically determine which actual resource file is in use in application? Would be useful to me for testing to send it out on debug log, so can be sure correct image file is being used i.e. I have set-up resource qualifiers correctly and placed correct images in correct folders.

Can do this manually, but as number of images increases this becomes a major chore.

I am guessing from similar questions about determining resource paths that this may not be possible. If not, does anyone have any tricks/tips? I could add an ID text to image (maybe even use a script to batch watermark images with filename) but would be happy to hear if there is a simpler way.


回答1:


If you wonder what PNG file that is currently (with the current configuration) loaded for, say R.drawable.icon, you can do this:

TypedValue returnedValue = new TypedValue();
getResources().openRawResource(R.drawable.icon, returnedValue);
Log.d("Example", "Path to loaded resource: " + returnedValue.string);

on an hdpi device, the output will be:

D/Example ( 2028): Path to loaded resource: res/drawable-hdpi/icon.png

This will give you the path to the actual resources used independently of how complex the path is. It only works for resources that are individual files within your .apk though, most notably drawables.




回答2:


Usually resource files will be automatically selected if u follow the android folder naming conventions in your app else you can get the resolution of the screen, height of the screen and width of the screen so on in the activity using the below APIs.

 ActivityContext.getResources().so on, press ctrl+space in eclipse for help      
 ActivityContext.getWindow().so on, press ctrl+space in eclipse for help



回答3:


U can do it using this but it works only if u follow the android folder naming conventions

 switch (getResources().getDisplayMetrics().densityDpi) {
                    case DisplayMetrics.DENSITY_LOW:
                        //Ldpi
                        break;
                    case DisplayMetrics.DENSITY_MEDIUM:
                        //Mdpi
                        break;
                    case DisplayMetrics.DENSITY_HIGH:
                        //Hdpi
                        break;
                    case DisplayMetrics.DENSITY_XHIGH:
                        //x-hdpi
                        break;
                    }


来源:https://stackoverflow.com/questions/8576224/how-to-identify-resource-file-in-use

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