I have made android app with jni C++ code.
To read model file while creating apk on android native code(C++
), I placed the model file in assets folder. (so file path is my_app/src/main/assets/model.tflite
)
Then, I tried to read the model with following code but an error has raised:
Kotlin
code:
private val context: Context
get() = getApplication<Application>().applicationContext
asset_manager = context.assets
C++
code:
AAssetManager* mgr = AAssetManager_fromJava(env, asset_manager);
AAssetDir* assetDir = AAssetManager_openDir(mgr, "");
const char* filename = (const char*)NULL;
while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL) {
AAsset* asset = AAssetManager_open(mgr, filename, AASSET_MODE_STREAMING);
__android_log_print(ANDROID_LOG_INFO, "tag", "file name is: %s", filename); <- file name reading is ok
char buf[BUFSIZ];
int nb_read = 0;
FILE* out = fopen(filename, "w"); <- out has null FILE*
while ((nb_read = AAsset_read(asset, buf, BUFSIZ)) > 0)
fwrite(buf, nb_read, 1, out);
fclose(out);
AAsset_close(asset);
}
AAssetDir_close(assetDir);
logcat
:
2022-06-19 22:45:54.449 12180-12180/com.my.first.app I/tag: file name is: model.tflite
2022-06-19 22:45:54.449 12180-12180/com.my.first.app A/libc: FORTIFY: fwrite: null FILE*
2022-06-19 22:45:54.450 12180-12180/com.my.first.app A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 12180 (st.app), pid 12180 (st.app)
Why does fopen(filename, "w")
return null
? It seems that AAssetManager
is work.