Android: Support file saving to internal data storage
This commit is contained in:
parent
966e8adcf9
commit
fb2ed693e4
3 changed files with 24 additions and 10 deletions
|
@ -650,7 +650,7 @@ void InitWindow(int width, int height, const char *title)
|
||||||
CORE.Android.app->onAppCmd = AndroidCommandCallback;
|
CORE.Android.app->onAppCmd = AndroidCommandCallback;
|
||||||
CORE.Android.app->onInputEvent = AndroidInputCallback;
|
CORE.Android.app->onInputEvent = AndroidInputCallback;
|
||||||
|
|
||||||
InitAssetManager(CORE.Android.app->activity->assetManager);
|
InitAssetManager(CORE.Android.app->activity->assetManager, CORE.Android.app->activity->internalDataPath);
|
||||||
|
|
||||||
TRACELOG(LOG_INFO, "Android app initialized successfully");
|
TRACELOG(LOG_INFO, "Android app initialized successfully");
|
||||||
|
|
||||||
|
|
28
src/utils.c
28
src/utils.c
|
@ -65,6 +65,7 @@ static TraceLogCallback logCallback = NULL; // Log callback function
|
||||||
|
|
||||||
#if defined(PLATFORM_ANDROID)
|
#if defined(PLATFORM_ANDROID)
|
||||||
static AAssetManager *assetManager = NULL; // Android assets manager pointer
|
static AAssetManager *assetManager = NULL; // Android assets manager pointer
|
||||||
|
static const char *internalDataPath = NULL; // Android internal data path
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(PLATFORM_UWP)
|
#if defined(PLATFORM_UWP)
|
||||||
|
@ -292,21 +293,34 @@ void SaveFileText(const char *fileName, char *text)
|
||||||
|
|
||||||
#if defined(PLATFORM_ANDROID)
|
#if defined(PLATFORM_ANDROID)
|
||||||
// Initialize asset manager from android app
|
// Initialize asset manager from android app
|
||||||
void InitAssetManager(AAssetManager *manager)
|
void InitAssetManager(AAssetManager *manager, const char *dataPath)
|
||||||
{
|
{
|
||||||
assetManager = manager;
|
assetManager = manager;
|
||||||
|
internalDataPath = dataPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replacement for fopen
|
// Replacement for fopen
|
||||||
|
// Ref: https://developer.android.com/ndk/reference/group/asset
|
||||||
FILE *android_fopen(const char *fileName, const char *mode)
|
FILE *android_fopen(const char *fileName, const char *mode)
|
||||||
{
|
{
|
||||||
if (mode[0] == 'w') return NULL;
|
if (mode[0] == 'w') // TODO: Test!
|
||||||
|
{
|
||||||
|
// TODO: fopen() is mapped to android_fopen() that only grants read access
|
||||||
|
// to assets directory through AAssetManager but we want to also be able to
|
||||||
|
// write data when required using the standard stdio FILE access functions
|
||||||
|
// Ref: https://stackoverflow.com/questions/11294487/android-writing-saving-files-from-native-code-only
|
||||||
|
#undef fopen
|
||||||
|
return fopen(TextFormat("%s/%s", internalDataPath, fileName), mode);
|
||||||
|
#define fopen(name, mode) android_fopen(name, mode)
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// NOTE: AAsset provides access to read-only asset
|
||||||
|
AAsset *asset = AAssetManager_open(assetManager, fileName, AASSET_MODE_UNKNOWN);
|
||||||
|
|
||||||
AAsset *asset = AAssetManager_open(assetManager, fileName, 0);
|
if (asset != NULL) return funopen(asset, android_read, android_write, android_seek, android_close);
|
||||||
|
else return NULL;
|
||||||
if (!asset) return NULL;
|
}
|
||||||
|
|
||||||
return funopen(asset, android_read, android_write, android_seek, android_close);
|
|
||||||
}
|
}
|
||||||
#endif // PLATFORM_ANDROID
|
#endif // PLATFORM_ANDROID
|
||||||
|
|
||||||
|
|
|
@ -68,8 +68,8 @@ extern "C" { // Prevents name mangling of functions
|
||||||
// Module Functions Declaration
|
// Module Functions Declaration
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
#if defined(PLATFORM_ANDROID)
|
#if defined(PLATFORM_ANDROID)
|
||||||
void InitAssetManager(AAssetManager *manager); // Initialize asset manager from android app
|
void InitAssetManager(AAssetManager *manager, const char *dataPath); // Initialize asset manager from android app
|
||||||
FILE *android_fopen(const char *fileName, const char *mode); // Replacement for fopen()
|
FILE *android_fopen(const char *fileName, const char *mode); // Replacement for fopen() -> Read-only!
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(PLATFORM_UWP)
|
#if defined(PLATFORM_UWP)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue