Accessing Assets

You have finished all the work for this chapter. You will develop BeatBox to actually use the content of your assets in the next chapter.

Before you do, though, let’s discuss a bit more about how assets work.

Your Sound object has an asset file path defined on it. Asset file paths will not work if you try to open them with a File; you must use them with an AssetManager:

S​t​r​i​n​g​ ​a​s​s​e​t​P​a​t​h​ ​=​ ​s​o​u​n​d​.​g​e​t​A​s​s​e​t​P​a​t​h​(​)​;​
I​n​p​u​t​S​t​r​e​a​m​ ​s​o​u​n​d​D​a​t​a​ ​=​ ​m​A​s​s​e​t​s​.​o​p​e​n​(​a​s​s​e​t​P​a​t​h​)​;​

This gives you a standard InputStream for the data, which you can use like any other InputStream in Java.

Some APIs require FileDescriptors instead. (This is what ...

Get Android Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.