title: App crashes using the File.listFiles() method if the folder contains files with special charaters
body: <p>In my project I have to iterate through files and directories. For that I am using <code>File.listFiles()</code> method.
  I am experiencing strange behavior with files which has special charters.</p>
  
  <h1>Background:</h1>
  
  <p>I have pushed a file using adb command to my device's Music folder.</p>
  
  <p>I am using windows machine.</p>
  
  <p>File name showing in windows <strong>"My_file_Ñ_’_Special_chars.mp3"</strong></p>
  
  <p>adb commad used to push file:</p>
  
  <pre><code>adb push "My_file_Ñ_’_Special_chars.mp3" /sdcard/Music/
  </code></pre>
  
  <p>After pushing file name showed in device: <strong>"/sdcard/Music/My_file_╤_Æ_Special_chars.mp3"</strong></p>
  
  <h1>Actual issue:</h1>
  
  <p>Now I am executing below code in my apk.</p>
  
  <pre><code>File myDir = new File("/sdcard/Music/");
  File[] files = myDir.listFiles();
  </code></pre>
  
  <p>Now my app is crashing at <strong>listFiles()</strong> method. Below I given crash logs.
  But if I copy same file using explorer(copy and paste) there is no issue.</p>
  
  <p>I got below links from stack but I didn't got any working solution.</p>
  
  <p><a href="https://stackoverflow.com/questions/47002551/file-listfiles-crashes-for-invalid-utf-8-characters">File.listFiles() crashes for invalid UTF-8 characters</a></p>
  
  <p><a href="https://stackoverflow.com/questions/3610013/file-listfiles-mangles-unicode-names-with-jdk-6-unicode-normalization-issues">File.listFiles() mangles unicode names with JDK 6 (Unicode Normalization issues)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/3610013/file-listfiles-mangles-unicode-names-with-jdk-6-unicode-normalization-issues">File.listFiles() mangles unicode names with JDK 6 (Unicode Normalization issues)</a></p>
  
  <p><strong>The issue is occurring on the devices having android version 7.0 and above.</strong></p>
  
  <h1>Crash logs:</h1>
  
  <pre><code>01-07 00:51:50.084 12871-12871/com.example.per.myapplication A/art: art/runtime/java_vm_ext.cc:470] JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal continuation byte 0x5f
      art/runtime/java_vm_ext.cc:470]     string: 'My_file_�_�_Special_chars.mp3'
      art/runtime/java_vm_ext.cc:470]     input: '0x4d 0x79 0x5f 0x66 0x69 0x6c 0x65 0x5f 0xd1 &lt;0x5f&gt; 0x92 0x5f 0x53 0x70 0x65 0x63 0x69 0x61 0x6c 0x5f 0x63 0x68 0x61 0x72 0x73 0x2e 0x6d 0x70 0x33'
      art/runtime/java_vm_ext.cc:470]     in call to NewStringUTF
      art/runtime/java_vm_ext.cc:470]     from java.lang.String[] java.io.UnixFileSystem.list0(java.io.File)
      art/runtime/java_vm_ext.cc:470] "main" prio=5 tid=1 Runnable
      art/runtime/java_vm_ext.cc:470]   | group="main" sCount=0 dsCount=0 obj=0x76257eb8 self=0x7f9f6c7a00
      art/runtime/java_vm_ext.cc:470]   | sysTid=12871 nice=0 cgrp=default sched=0/0 handle=0x7fa3ec2a98
      art/runtime/java_vm_ext.cc:470]   | state=R schedstat=( 489853444 94436765 524 ) utm=37 stm=11 core=1 HZ=100
      art/runtime/java_vm_ext.cc:470]   | stack=0x7fe0245000-0x7fe0247000 stackSize=8MB
      art/runtime/java_vm_ext.cc:470]   | held mutexes= "mutator lock"(shared held)
      art/runtime/java_vm_ext.cc:470]   native: #00 pc 0000000000477fdc  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+220)
      art/runtime/java_vm_ext.cc:470]   native: #01 pc 0000000000477fd8  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+216)
      art/runtime/java_vm_ext.cc:470]   native: #02 pc 000000000044c54c  /system/lib64/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+472)
      art/runtime/java_vm_ext.cc:470]   native: #03 pc 00000000002ed300  /system/lib64/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+1128)
      art/runtime/java_vm_ext.cc:470]   native: #04 pc 00000000002ed9e8  /system/lib64/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+120)
      art/runtime/java_vm_ext.cc:470]   native: #05 pc 0000000000101b00  /system/lib64/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+156)
      art/runtime/java_vm_ext.cc:470]   native: #06 pc 00000000001027fc  /system/lib64/libart.so (_ZN3art11ScopedCheck14CheckUtfStringEPKcb+1344)
      art/runtime/java_vm_ext.cc:470]   native: #07 pc 00000000000ff048  /system/lib64/libart.so (_ZN3art11ScopedCheck5CheckERNS_18ScopedObjectAccessEbPKcPNS_12JniValueTypeE+1120)
      art/runtime/java_vm_ext.cc:470]   native: #08 pc 00000000000f9b88  /system/lib64/libart.so (_ZN3art8CheckJNI12NewStringUTFEP7_JNIEnvPKc+624)
      art/runtime/java_vm_ext.cc:470]   native: #09 pc 0000000000021944  /system/lib64/libopenjdk.so (Java_java_io_UnixFileSystem_list0+468)
      art/runtime/java_vm_ext.cc:470]   native: #10 pc 0000000000064fcc  /system/framework/arm64/boot.oat (Java_java_io_UnixFileSystem_list0__Ljava_io_File_2+152)
      art/runtime/java_vm_ext.cc:470]   at java.io.UnixFileSystem.list0(Native method)
      art/runtime/java_vm_ext.cc:470]   at java.io.UnixFileSystem.list(UnixFileSystem.java:301)
      art/runtime/java_vm_ext.cc:470]   at java.io.File.list(File.java:1059)
      art/runtime/java_vm_ext.cc:470]   at java.io.File.listFiles(File.java:1137)
      art/runtime/java_vm_ext.cc:470]   at com.example.per.myapplication.MainActivity$1.onClick(MainActivity.java:52)
      art/runtime/java_vm_ext.cc:470]   at android.view.View.performClick(View.java:6213)
      art/runtime/java_vm_ext.cc:470]   at android.widget.TextView.performClick(TextView.java:11074)
      art/runtime/java_vm_ext.cc:470]   at android.view.View$PerformClick.run(View.java:23645)
      art/runtime/java_vm_ext.cc:470]   at android.os.Handler.handleCallback(Handler.java:751)
      art/runtime/java_vm_ext.cc:470]   at android.os.Handler.dispatchMessage(Handler.java:95)
      art/runtime/java_vm_ext.cc:470]   at android.os.Looper.loop(Looper.java:154)
      art/runtime/java_vm_ext.cc:470]   at android.app.ActivityThread.main(ActivityThread.java:6646)
      art/runtime/java_vm_ext.cc:470]   at java.lang.reflect.Method.invoke!(Native method)
      art/runtime/java_vm_ext.cc:470]   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
      art/runtime/java_vm_ext.cc:470]   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
      art/runtime/java_vm_ext.cc:470] 
  01-07 00:51:50.160 12871-12871/com.example.per.myapplication A/art: art/runtime/runtime.cc:403] Runtime aborting...
      art/runtime/runtime.cc:403] Aborting thread:
      art/runtime/runtime.cc:403] "main" prio=5 tid=1 Native
      art/runtime/runtime.cc:403]   | group="" sCount=0 dsCount=0 obj=0x76257eb8 self=0x7f9f6c7a00
      art/runtime/runtime.cc:403]   | sysTid=12871 nice=0 cgrp=default sched=0/0 handle=0x7fa3ec2a98
      art/runtime/runtime.cc:403]   | state=R schedstat=( 512948081 94540411 525 ) utm=37 stm=14 core=1 HZ=100
      art/runtime/runtime.cc:403]   | stack=0x7fe0245000-0x7fe0247000 stackSize=8MB
      art/runtime/runtime.cc:403]   | held mutexes= "abort lock"
      art/runtime/runtime.cc:403]   native: #00 pc 0000000000477fdc  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+220)
      art/runtime/runtime.cc:403]   native: #01 pc 0000000000477fd8  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+216)
      art/runtime/runtime.cc:403]   native: #02 pc 000000000044c54c  /system/lib64/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+472)
      art/runtime/runtime.cc:403]   native: #03 pc 000000000043a5b4  /system/lib64/libart.so (_ZNK3art10AbortState10DumpThreadERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEPNS_6ThreadE+56)
      art/runtime/runtime.cc:403]   native: #04 pc 000000000043a430  /system/lib64/libart.so (_ZNK3art10AbortState4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+668)
      art/runtime/runtime.cc:403]   native: #05 pc 000000000042e0a4  /system/lib64/libart.so (_ZN3art7Runtime5AbortEv+140)
      art/runtime/runtime.cc:403]   native: #06 pc 00000000000e4ea4  /system/lib64/libart.so (_ZN3art10LogMessageD2Ev+1204)
      art/runtime/runtime.cc:403]   native: #07 pc 00000000002ed714  /system/lib64/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+2172)
      art/runtime/runtime.cc:403]   native: #08 pc 00000000002ed9e8  /system/lib64/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+120)
      art/runtime/runtime.cc:403]   native: #09 pc 0000000000101b00  /system/lib64/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+156)
      art/runtime/runtime.cc:403]   native: #10 pc 00000000001027fc  /system/lib64/libart.so (_ZN3art11ScopedCheck14CheckUtfStringEPKcb+1344)
      art/runtime/runtime.cc:403]   native: #11 pc 00000000000ff048  /system/lib64/libart.so (_ZN3art11ScopedCheck5CheckERNS_18ScopedObjectAccessEbPKcPNS_12JniValueTypeE+1120)
      art/runtime/runtime.cc:403]   native: #12 pc 00000000000f9b88  /system/lib64/libart.so (_ZN3art8CheckJNI12NewStringUTFEP7_JNIEnvPKc+624)
      art/runtime/runtime.cc:403]   native: #13 pc 0000000000021944  /system/lib64/libopenjdk.so (Java_java_io_UnixFileSystem_list0+468)
      art/runtime/runtime.cc:403]   native: #14 pc 0000000000064fcc  /system/framework/arm64/boot.oat (???)
      art/runtime/runtime.cc:403]   at java.io.UnixFileSystem.list0(Native method)
      art/runtime/runtime.cc:403]   at java.io.UnixFileSystem.list(UnixFileSystem.java:301)
      art/runtime/runtime.cc:403]   at java.io.File.list(File.java:1059)
      art/runtime/runtime.cc:403]   at java.io.File.listFiles(File.java:1137)
      art/runtime/runtime.cc:403]   at com.example.per.myapplication.MainActivity$1.onClick(MainActivity.java:52)
      art/runtime/runtime.cc:403]   at android.view.View.performClick(View.java:6213)
      art/runtime/runtime.cc:403]   at android.widget.TextView.performClick(TextView.java:11074)
      art/runtime/runtime.cc:403]   at android.view.View$PerformClick.run(View.java:23645)
      art/runtime/runtime.cc:403]   at android.os.Handler.handleCallback(Handler.java:751)
      art/runtime/runtime.cc:403]   at android.os.Handler.dispatchMessage(Handler.java:95)
      art/runtime/runtime.cc:403]   at android.os.Looper.loop(Looper.java:154)
      art/runtime/runtime.cc:403]   at android.app.ActivityThread.main(ActivityThread.java:6646)
      art/runtime/runtime.cc:403]   at java.lang.reflect.Method.invoke!(Native method)
      art/runtime/runtime.cc:403]   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
      art/runtime/runtime.cc:403]   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
      art/runtime/runtime.cc:403] Dumping all threads without appropriate locks held: thread list lock mutator lock
      art/runtime/runtime.cc:403] All threads:
      art/runtime/runtime.cc:403] DALVIK THREADS (15):
      art/runtime/runtime.cc:403] "main" prio=5 tid=1 Runnable
      art/runtime/runtime.cc:403]   | group="" sCount=0 dsCount=0 obj=0x76257eb8 self=0x7f9f6c7a00
      art/runtime/runtime.cc:403]   | sysTid=12871 nice=0 cgrp=default sched=0/0 handle=0x7fa3ec2a98
      art/runtime/runtime.cc:403]   | state=R schedstat=( 551184387 95686296 530 ) utm=38 stm=17 core=2 HZ=100
      art/runtime/runtime.cc:403]   | stack=0x7fe0245000-0x7fe0247000 stackSize=8MB
      art/runtime/runtime.cc:403]   | held mutexes= "abort lock" "mutator lock"(shared held)
      art/runtime/runtime.cc:403]   native: #00 pc 0000000000477fdc  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+220)
      art/runtime/runtime.cc:403]   native: #01 pc 0000000000477fd8  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+216)
      art/runtime/runtime.cc:403]   native: #02 pc 000000000044c54c  /system/lib64/libart.so (_ZNK3art6Thread9DumpStackERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEbP12BacktraceMap+472)
      art/runtime/runtime.cc:403]   native: #03 pc 0000000000463f44  /system/lib64/libart.so (_ZN3art14DumpCheckpoint3RunEPNS_6ThreadE+820)
      art/runtime/runtime.cc:403]   native: #04 pc 000000000045bf4c  /system/lib64/libart.so (_ZN3art10ThreadList13RunCheckpointEPNS_7ClosureE+456)
      art/runtime/runtime.cc:403]   native: #05 pc 000000000045bb5c  /system/lib64/libart.so (_ZN3art10ThreadList4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEb+288)
      art/runtime/runtime.cc:403]   native: #06 pc 000000000043a3e4  /system/lib64/libart.so (_ZNK3art10AbortState4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+592)
      art/runtime/runtime.cc:403]   native: #07 pc 000000000042e0a4  /system/lib64/libart.so (_ZN3art7Runtime5AbortEv+140)
      art/runtime/runtime.cc:403]   native: #08 pc 00000000000e4ea4  /system/lib64/libart.so (_ZN3art10LogMessageD2Ev+1204)
      art/runtime/runtime.cc:403]   native: #09 pc 00000000002ed714  /system/lib64/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+2172)
      art/runtime/runtime.cc:403]   native: #10 pc 00000000002ed9e8  /system/lib64/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+120)
      art/runtime/runtime.cc:403]   native: #11 pc 0000000000101b00  /system/lib64/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+156)
      art/runtime/runtime.cc:403]   native: #12 pc 00000000001027fc  /system/lib64/libart.so (_ZN3art11ScopedCheck14CheckUtfStringEPKcb+1344)
      art/runtime/runtime.cc:403]   native: #13 pc 00000000000ff048  /system/lib64/libart.so (_ZN3art11ScopedCheck5CheckERNS_18ScopedObjectAccessEbPKcPNS_12JniValueTypeE+1120)
      art/runtime/runtime.cc:403]   native: #14 pc 00000000000f9b88  /system/lib64/libart.so (_ZN3art8CheckJNI12NewStringUTFEP7_JNIEnvPKc+624)
      art/runtime/runtime.cc:403]   native: #15 pc 0000000000021944  /system/lib64/libopenjdk.so (Java_java_io_UnixFileSystem_list0+468)
      art/runtime/runtime.cc:403]   native: #16 pc 0000000000064fcc  /system/framework/arm64/boot.oat (Java_java_io_UnixFileSystem_list0__Ljava_io_File_2+152)
      art/runtime/runtime.cc:403]   at java.io.UnixFileSystem.list0(Native method)
      art/runtime/runtime.cc:403]   at java.io.UnixFileSystem.list(UnixFileSystem.java:301)
      art/runtime/runtime.cc:403]   at java.io.File.list(File.java:1059)
      art/runtime/runtime.cc:403]   at java.io.File.listFiles(File.java:1137)
      art/runtime/runtime.cc:403]   at com.example.per.myapplication.MainActivity$1.onClick(MainActivity.java:52)
      art/runtime/runtime.cc:403]   at android.view.View.performClick(View.java:6213)
      art/runtime/runtime.cc:403]   at android.widget.TextView.performClick(TextView.java:11074)
      art/runtime/runtime.cc:403]   at android.view.View$PerformClick.run(View.java:23645)
      art/runtime/runtime.cc:403]   at android.os.Handler.handleCallback(Handler.java:751)
      art/runtime/runtime.cc:403]   at android.os.Handler.dispatchMessage(Handler.java:95)
  01-07 00:51:50.161 12871-12871/com.example.per.myapplication A/art: art/runtime/runtime.cc:403]   at android.os.Looper.loop(Looper.java:154)
      art/runtime/runtime.cc:403]   at android.app.ActivityThread.main(ActivityThread.java:6646)
      art/runtime/runtime.cc:403]   at java.lang.reflect.Method.invoke!(Native method)
      art/runtime/runtime.cc:403]   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
      art/runtime/runtime.cc:403]   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
      art/runtime/runtime.cc:403] 
  
      --------- beginning of crash
  01-07 00:51:50.166 12871-12871/com.example.per.myapplication A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 12871 (r.myapplication)
  </code></pre>