Merge pull request #1189 from Lugdunum3D/fix-android-asset-extraction

Create the directory for the asset's extraction on Android
pull/1193/head
Kim Kulling 2017-03-01 00:21:32 +01:00 committed by GitHub
commit 5c45b408d0
1 changed files with 32 additions and 0 deletions

View File

@ -46,7 +46,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <android/api-level.h>
#if __ANDROID__ and __ANDROID_API__ > 9 and defined(AI_CONFIG_ANDROID_JNI_ASSIMP_MANAGER_SUPPORT)
#include <libgen.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <android/log.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
@ -100,6 +102,27 @@ void AndroidJNIIOSystem::AndroidActivityInit(ANativeActivity* activity)
mApkAssetManager = activity->assetManager;
}
// ------------------------------------------------------------------------------------------------
// Create the directory for the extracted resource
static int mkpath(std::string path, mode_t mode)
{
if (mkdir(path.c_str(), mode) == -1) {
switch(errno) {
case ENOENT:
if (mkpath(path.substr(0, path.find_last_of('/')), mode) == -1)
return -1;
else
return mkdir(path.c_str(), mode);
case EEXIST:
return 0;
default:
return -1;
}
}
return 0;
}
// ------------------------------------------------------------------------------------------------
// Extracts android asset
bool AndroidJNIIOSystem::AndroidExtractAsset(std::string name)
@ -131,6 +154,15 @@ bool AndroidJNIIOSystem::AndroidExtractAsset(std::string name)
// Close
AAsset_close(asset);
// Prepare directory for output buffer
std::string directoryNewPath = newPath;
directoryNewPath = dirname(&directoryNewPath[0]);
if (mkpath(directoryNewPath, S_IRUSR | S_IWUSR | S_IXUSR) == -1) {
__android_log_print(ANDROID_LOG_ERROR, "assimp",
"Can not create the directory for the output file");
}
// Prepare output buffer
std::ofstream assetExtracted(newPath.c_str(),
std::ios::out | std::ios::binary);