44#include < jni.h>
55#include < time.h>
66#include < android/log.h>
7+ #include < android/api-level.h>
78
89
910#include < stdio.h>
@@ -825,8 +826,9 @@ JNIEXPORT jboolean JNICALL Java_org_coolreader_crengine_Engine_checkFontLanguage
825826 * Signature: (Ljava/io/File;)[Ljava/io/File;
826827 */
827828JNIEXPORT jobjectArray JNICALL Java_org_coolreader_crengine_Engine_listFilesInternal
828- (JNIEnv *env , jclass, jobject jdir)
829+ (JNIEnv *penv , jclass, jobject jdir)
829830{
831+ CRJNIEnv env (penv);
830832 if (NULL == jdir)
831833 return NULL ;
832834 jclass pjcFile = env->FindClass (" java/io/File" );
@@ -839,10 +841,9 @@ JNIEXPORT jobjectArray JNICALL Java_org_coolreader_crengine_Engine_listFilesInte
839841 if (NULL == pjmFile_Ctor)
840842 return NULL ;
841843 jstring jpathname = (jstring)env->CallObjectMethod (jdir, pjmFile_GetAbsolutePath);
842- jboolean iscopy;
843- const char * s = env->GetStringUTFChars (jpathname, &iscopy);
844- lString16 path = (CRJNIEnv::sdk_int >= ANDROID_SDK_M) ? Utf8ToUnicode (s) : Wtf8ToUnicode (s);
845- env->ReleaseStringUTFChars (jpathname, s);
844+ if (NULL == jpathname)
845+ return NULL ;
846+ lString16 path = env.fromJavaString (jpathname);
846847 jobjectArray jarray = NULL ;
847848 LVContainerRef dir = LVOpenDirectory (path);
848849 if ( !dir.isNull () ) {
@@ -852,13 +853,22 @@ JNIEXPORT jobjectArray JNICALL Java_org_coolreader_crengine_Engine_listFilesInte
852853 if (NULL != jarray) {
853854 for (int i = 0 ; i < dir->GetObjectCount (); i++) {
854855 const LVContainerItemInfo *item = dir->GetObjectInfo (i);
855- lString16 fileName = path + " /" + item->GetName ();
856- jstring jfilename = env->NewStringUTF (
857- (CRJNIEnv::sdk_int >= ANDROID_SDK_M) ? UnicodeToUtf8 (fileName).c_str ()
858- : UnicodeToWtf8 (fileName).c_str ());
859- jobject jfile = env->NewObject (pjcFile, pjmFile_Ctor, jfilename);
860- if (NULL != jfile)
861- env->SetObjectArrayElement (jarray, i, jfile);
856+ if (item && item->GetName ()) {
857+ lString16 fileName = path + " /" + item->GetName ();
858+ jstring jfilename = env.toJavaString (fileName);
859+ if (NULL != jfilename) {
860+ env->ExceptionClear ();
861+ jobject jfile = env->NewObject (pjcFile, pjmFile_Ctor, jfilename);
862+ if (env->ExceptionCheck () == JNI_TRUE)
863+ env->ExceptionClear ();
864+ else {
865+ if (NULL != jfile)
866+ env->SetObjectArrayElement (jarray, i, jfile);
867+ }
868+ env->DeleteLocalRef (jfile);
869+ env->DeleteLocalRef (jfilename);
870+ }
871+ }
862872 }
863873 }
864874 }
0 commit comments