How to refer to a resource not in the Library Project, but only in the dependent Application Project?

谁说胖子不能爱 提交于 2019-12-11 09:08:41

问题


I'm turning my one app into several similar apps and using a base library project. I want to load a video that will be in res/raw/welcome.m4a in the dependent applications.

This is how is was being done before using the library project:

vv.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.welcome));

This produces an error in my library project, because res/raw/welcome.m4v doesn't exist, it will only be in the App Project res/ folder, and will be different for each app.

My current workaround is to create a dummy res/raw/welcome.m4v in the base library project so it stops complaining.

So my question is how do I have code in the Library Project that refers to resources that don't exist in the Library Project?


回答1:


If it is only happening infrequently, you can try this:

vv.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+getResources().getIdentifier("welcome", "raw", getPackageName())));

Not as performant as using the id, but it should work.



来源:https://stackoverflow.com/questions/17004739/how-to-refer-to-a-resource-not-in-the-library-project-but-only-in-the-dependent

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