How to identify resource file in use?

2019-05-29 09:54发布

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.

3条回答
相关推荐>>
2楼-- · 2019-05-29 10:35

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.

查看更多
Evening l夕情丶
3楼-- · 2019-05-29 10:35

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
查看更多
时光不老,我们不散
4楼-- · 2019-05-29 10:49

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;
                    }
查看更多
登录 后发表回答