in PlayAssetDelivery/NativeSample/common/ndk_helper/JNIHelper.cpp [133:195]
bool JNIHelper::ReadFile(const char* fileName,
std::vector<uint8_t>* buffer_ref) {
if (activity_ == NULL) {
LOGI(
"JNIHelper has not been initialized.Call init() to initialize the "
"helper");
return false;
}
// Lock mutex
std::lock_guard<std::mutex> lock(mutex_);
// First, try reading from externalFileDir;
JNIEnv* env = AttachCurrentThread();
jstring str_path = GetExternalFilesDirJString(env);
std::string s;
if(str_path) {
const char* path = env->GetStringUTFChars(str_path, NULL);
s = std::string(path);
if (fileName[0] != '/') {
s.append("/");
}
s.append(fileName);
env->ReleaseStringUTFChars(str_path, path);
env->DeleteLocalRef(str_path);
}
std::ifstream f(s.c_str(), std::ios::binary);
activity_->vm->DetachCurrentThread();
if (f) {
LOGI("reading:%s", s.c_str());
f.seekg(0, std::ifstream::end);
int32_t fileSize = f.tellg();
f.seekg(0, std::ifstream::beg);
buffer_ref->reserve(fileSize);
buffer_ref->assign(std::istreambuf_iterator<char>(f),
std::istreambuf_iterator<char>());
f.close();
return true;
} else {
// Fallback to assetManager
AAssetManager* assetManager = activity_->assetManager;
AAsset* assetFile =
AAssetManager_open(assetManager, fileName, AASSET_MODE_BUFFER);
if (!assetFile) {
return false;
}
uint8_t* data = (uint8_t*)AAsset_getBuffer(assetFile);
int32_t size = AAsset_getLength(assetFile);
if (data == NULL) {
AAsset_close(assetFile);
LOGI("Failed to load:%s", fileName);
return false;
}
buffer_ref->reserve(size);
buffer_ref->assign(data, data + size);
AAsset_close(assetFile);
return true;
}
}