- Home /
Where to store files in Android that can be accessed later via path?
I have an audio file that I want to be able to access later via a path. To the curious mind, I need that audio file in an FFmpeg command. streamingAssetsPath and persistentDataPath do not work in Android. If I use streamingAssetsPath, I get "Protocol not found" error. If I use persistentDataPath, I get "No such file or directory" error.
i have used multiple times persistentDataPath in android and never had an issue, make sure the file you are searching there exists. and for strea$$anonymous$$g assets is because is inside a jar and you need to access it using the www class for example.
If I use persistentDataPath, where should I put the audio files in the project folder? For instance, strea$$anonymous$$gAssetsPath points to "\Assets\Strea$$anonymous$$gAssets".
Answer by Bunny83 · Feb 05, 2019 at 04:41 PM
You have to understand how Android handles apps internally. Your app is essentially just a single zip archive, the APK file that you create. It contains everything that is related to your app inside this archive. However it should be clear that when installing an app the only actual file that is put on the users device is that one APK file. So there is no place where you can put a file that appears automagically on the users device when you install the app.
However if you need a certain asset as a standalone file on the device, you have to create that file manually. A common way is to place the file in the streaming asset path of your project. This way the file doesn't get compiled into Unity's internal assetdatabase but it's simply packed inside the APK archive in a special subfolder. The only ways to get access to files inside the APK is to either use Unity's WWW class which can handle the special file URIs with the custom scheme jar:file://
. Another way is to use any third party library which can read / handle comressed files. Once you can read the content, you can simply store it as actual file inside the persistant data path,
So the usual process is like this:
try reading the file from the persistant data path.
if the file doesn't exist yet, "extract" the file from the streaming assets path using the WWW class and save it to the persistant data path.
So your file will be extracted at the first run or whenever the file gets deleted from the persistant data path.
Thanks for the long response. I am aware of how it works in Android. I have tried all of them and none of it seem to work. And that's why I'm here. I guess there's really no choice but to manually place the files in the persistentDataPath on runtime on the first run.
What do you mean by "tried all of them"? Bunny's response seems pretty thorough to me. I've used what's described above as "usual process" many many times and it works fine.
We have the same hack/solution. Extract the files from strea$$anonymous$$g assets and then save to persistent data path on the first run. This is the solution I'm trying to avoid. When I said "tried all of them" I meant I've tried everything I know how to avoid the hack.
Your answer
Follow this Question
Related Questions
Why am I unable to locate font files(.ttf) in unity android game in persistentDataPath? 1 Answer
Loading an image from streamingassets fails every time on Android 1 Answer
Open an HTML file from streamingAssets - Android 0 Answers
loading Images from streaming assets in Android 0 Answers
StreamingAssets and reading a binary file (Android) 0 Answers