mirror of
https://github.com/android/ndk-samples
synced 2025-11-10 18:41:39 +08:00
handles the case when getExternalFilesDir() returns NULL case in JNIHelper
this avoid the hang when running on android-17
This commit is contained in:
@@ -140,21 +140,21 @@ bool JNIHelper::ReadFile(const char* fileName,
|
|||||||
|
|
||||||
// First, try reading from externalFileDir;
|
// First, try reading from externalFileDir;
|
||||||
JNIEnv* env = AttachCurrentThread();
|
JNIEnv* env = AttachCurrentThread();
|
||||||
|
|
||||||
jstring str_path = GetExternalFilesDirJString(env);
|
jstring str_path = GetExternalFilesDirJString(env);
|
||||||
const char* path = env->GetStringUTFChars(str_path, NULL);
|
|
||||||
std::string s(path);
|
|
||||||
|
|
||||||
|
std::string s;
|
||||||
|
if(str_path) {
|
||||||
|
const char* path = env->GetStringUTFChars(str_path, NULL);
|
||||||
|
s = std::string(path);
|
||||||
if (fileName[0] != '/') {
|
if (fileName[0] != '/') {
|
||||||
s.append("/");
|
s.append("/");
|
||||||
}
|
}
|
||||||
s.append(fileName);
|
s.append(fileName);
|
||||||
std::ifstream f(s.c_str(), std::ios::binary);
|
|
||||||
|
|
||||||
env->ReleaseStringUTFChars(str_path, path);
|
env->ReleaseStringUTFChars(str_path, path);
|
||||||
env->DeleteLocalRef(str_path);
|
env->DeleteLocalRef(str_path);
|
||||||
|
}
|
||||||
|
std::ifstream f(s.c_str(), std::ios::binary);
|
||||||
activity_->vm->DetachCurrentThread();
|
activity_->vm->DetachCurrentThread();
|
||||||
|
|
||||||
if (f) {
|
if (f) {
|
||||||
LOGI("reading:%s", s.c_str());
|
LOGI("reading:%s", s.c_str());
|
||||||
f.seekg(0, std::ifstream::end);
|
f.seekg(0, std::ifstream::end);
|
||||||
@@ -520,16 +520,18 @@ jstring JNIHelper::GetExternalFilesDirJString(JNIEnv* env) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jstring obj_Path = nullptr;
|
||||||
// Invoking getExternalFilesDir() java API
|
// Invoking getExternalFilesDir() java API
|
||||||
jclass cls_Env = env->FindClass(NATIVEACTIVITY_CLASS_NAME);
|
jclass cls_Env = env->FindClass(NATIVEACTIVITY_CLASS_NAME);
|
||||||
jmethodID mid = env->GetMethodID(cls_Env, "getExternalFilesDir",
|
jmethodID mid = env->GetMethodID(cls_Env, "getExternalFilesDir",
|
||||||
"(Ljava/lang/String;)Ljava/io/File;");
|
"(Ljava/lang/String;)Ljava/io/File;");
|
||||||
jobject obj_File = env->CallObjectMethod(activity_->clazz, mid, NULL);
|
jobject obj_File = env->CallObjectMethod(activity_->clazz, mid, NULL);
|
||||||
|
if (obj_File) {
|
||||||
jclass cls_File = env->FindClass("java/io/File");
|
jclass cls_File = env->FindClass("java/io/File");
|
||||||
jmethodID mid_getPath =
|
jmethodID mid_getPath =
|
||||||
env->GetMethodID(cls_File, "getPath", "()Ljava/lang/String;");
|
env->GetMethodID(cls_File, "getPath", "()Ljava/lang/String;");
|
||||||
jstring obj_Path = (jstring)env->CallObjectMethod(obj_File, mid_getPath);
|
obj_Path = (jstring)env->CallObjectMethod(obj_File, mid_getPath);
|
||||||
|
}
|
||||||
return obj_Path;
|
return obj_Path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user