在我的项目中,我必须遍历文件和目录。为此,我正在使用File.listFiles()
方法。我的文件包含特殊章程,因此出现异常行为。
我已使用adb命令将文件推送到设备的“音乐”文件夹。
我正在使用Windows计算机。
在Windows中显示的文件名“My_file_Ñ_’_ Special_chars.mp3”
adb常用来推送文件:
adb push "My_file_Ñ_’_Special_chars.mp3" /sdcard/Music/
推送设备中显示的文件名后:“ / sdcard / Music /My_file_╤_Æ_Special_chars.mp3”
现在我正在执行apk中的以下代码。
File myDir = new File("/sdcard/Music/");
File[] files = myDir.listFiles();
现在我的应用程序在listFiles()方法处崩溃。下面我给出了崩溃日志。但是,如果我使用资源管理器复制同一文件(复制并粘贴),则没有问题。
我从堆栈中获得了下面的链接,但没有任何有效的解决方案。
File.listFiles() crashes for invalid UTF-8 characters
File.listFiles() mangles unicode names with JDK 6 (Unicode Normalization issues)
File.listFiles() mangles unicode names with JDK 6 (Unicode Normalization issues)
问题在具有android 7.0及更高版本的设备上发生。
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 <0x5f> 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)
此错误发生在调试应用程序版本中,编译为发行版。