简体   繁体   中英

How to include raw files without Android Studio/XML?

I'm working with Sublime Text & a command line ANT build script. How do I include data files that I can access and read through code? I tried using getResources().openRawResource(); but the parameter is an int, not a string. Also, I do not have the R class because it gets generated automatically. I also don't have any XML files besides AndroidManifest.

How to include raw files without Android Studio/XML?

The same ways that you do with Android Studio. Either:

  • Add them as raw resources (eg, to res/raw/ ) and access them via getResources().openRawResource() , or

  • Add them as assets (to an assets/ directory, the location of which depends on your Ant(?!?) script) and access them via getAssets().open()

I tried using getResources().openRawResource(); but the parameter is an int, not a string.

Correct. That is a resource ID. For a raw resource, it will be R.raw.something , where something is the base name of the resource file. So, for a res/raw/foo.mp3 file, you would use R.raw.foo . This is no different than layout resources ( R.layout ), drawable resources ( R.drawable ), and every other type of resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM