Skip to content

Commit

Permalink
Merge pull request assimp#1189 from Lugdunum3D/fix-android-asset-extr…
Browse files Browse the repository at this point in the history
…action

Create the directory for the asset's extraction on Android
  • Loading branch information
kimkulling authored Feb 28, 2017
2 parents 9a0a74e + cf68e03 commit 5c45b40
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions port/AndroidJNI/AndroidJNIIOSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5c45b40

Please sign in to comment.