Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Writing files on Android

This thread is locked; no one can reply to it. rss feed Print
Writing files on Android
Max Savenkov
Member #4,613
May 2004
avatar

1) apk_file_interface has no way of writing files. Should I always switch back to standard_file_interface when I want to write a file (implicitly though al_save_config_file or explicitly through al_fopen), and then, once more, to apk_file_interface? This seem to complicate code a lot! Is there any way to avoid it?

2) Despite what comment in android_system.c says, Allegro does not return external storage dir (sdcard) for ALLEGRO_TEMP_PATH, ALLEGRO_USER_DATA_PATH, ALLEGRO_USER_HOME_PATH, ALLEGRO_USER_SETTINGS_PATH or ALLEGRO_USER_DOCUMENTS_PATH. Instead, it returns path to Internal Storage. Indeed, I see no way to get a path to External Storage, even though AllegroActivity prints it to log, and checks if it is mounted and writeable. Is this intentional?

And an unrelated question: why doesn't Allegro use AAssetManager_* functions directly instead of JNI calls to work with files?

Thomas Fjellstrom
Member #476
June 2000
avatar

And an unrelated question: why doesn't Allegro use AAssetManager_* functions directly instead of JNI calls to work with files?

I'm not sure those C APIs existed when the port was started. Also the NDK apis can be somewhat annoying to work with. I haven't looked at the AssetManager api, but trying to do much with the main display stuff was a larger pain than using jni (no joke) for allegro's use case.

1. No real way to avoid switching. You can only access what's in an apk using the apk file interface, and you can only access the main fs using the stdio interface.

2. It looks like we don't return any sd card paths. Though we probably should somewhere, but i'm not sure where. Maybe a new "EXTERNAL" path?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Max Savenkov
Member #4,613
May 2004
avatar

I'm not sure those C APIs existed when the port was started

It marked as API 1, so I guess it always was there, but who knows now. I only learned about it myself today, when I saw it in code at my new work :)

Quote:

2. It looks like we don't return any sd card paths. Though we probably should somewhere, but i'm not sure where. Maybe a new "EXTERNAL" path?

Yes, I think a separate type of path seems to be the way to go.

Thomas Fjellstrom
Member #476
June 2000
avatar

It marked as API 1, so I guess it always was there, but who knows now. I only learned about it myself today, when I saw it in code at my new work :)

It's been years, and i can't really remember looking at it, but I probably did, and had a reason for not using it. I think it just accesses the java AssetManager class, which I think only does apk or those weird "raw" bundle files.

Quote:

Yes, I think a separate type of path seems to be the way to go.

It would literally only do anything special on Android though. Might be best to make a more generic one, that could provide a path different from the current ones on some/most platforms, but I'm not sure what it'd do.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Max Savenkov
Member #4,613
May 2004
avatar

It's been years, and i can't really remember looking at it, but I probably did, and had a reason for not using it. I think it just accesses the java AssetManager class, which I think only does apk or those weird "raw" bundle files.

I'm only asking because these functions does not seem to have the same problems with compressed assets that Allegro's implementation has... But maybe I'm wrong.

Quote:

It would literally only do anything special on Android though. Might be best to make a more generic one, that could provide a path different from the current ones on some/most platforms, but I'm not sure what it'd do.

I can't quite imagine it being useful on anything, but a mobile platform with SD card support. That being said, while iOS doesn't support them, Windows Phone does (if Allegro ever expands to it - and it may, what with the recently announced Android compatibility!), and I think Ubuntu Phones will have them too (I plan on getting Meizu MX4 Ubuntu as soon as it cames out, just to try something slightly different from the usual locked-down mobile OSes).

Thomas Fjellstrom
Member #476
June 2000
avatar

I'm only asking because these functions does not seem to have the same problems with compressed assets that Allegro's implementation has... But maybe I'm wrong.

I'm not sure. It looks like a direct passthrough of the java AssetManager which is what we use for the apk driver.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Max Savenkov
Member #4,613
May 2004
avatar

The call chain seem to end at Asset.cpp (https://github.com/android/platform_frameworks_base/blob/master/libs/androidfw/Asset.cpp) and various calls to libc functions. Though, there is a catch: when opening an asset from apk, this class likes to mmap it into memory, unless otherwise specified (I believe one needs to pass ACCESS_UNKNOWN as the mode argument to prevent this). Which may lead to crashes when reading really big files (at work, we have a single 200Mb file inside APK where all assets are stored in custom format, and opening it crashes on low-end devices with out of memory errors).

Thomas Fjellstrom
Member #476
June 2000
avatar

That's strange, an mmap shouldn't cause it to be read into memory on map. only pages at a time should be when you try to access it.

Anyhow, either we extend our code to work with compressed assets (and potentially .gz files, neither of which is that hard), or we just use AAssetManager instead.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Max Savenkov
Member #4,613
May 2004
avatar

I fixed up a solution for compressed assets problem in my local copy of Allegro, because I needed it recently, but it's not very pretty. Allegro only want uncompressed assets to know their size beforehand, and the only way to know size of compressed/streaming assets is to read them to the end. But size of file isn't always needed. So, I made Allegro compute it lazily for compressed assets, only when it is requested. But then, Allegro does it the slow way, by reading to the end of stream and rewinding it to the starting position (in case someone asked a file size in the middle of reading it).

Go to: