有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Android查找文件扩展名

因此,我试图从文件中获取扩展名,以便将其与字符串进行比较,以查看扩展名是否与我要查找的扩展名匹配。然而,目前我认为我能够获得扩展,但将其与我想要找到的扩展进行比较会导致我的应用程序崩溃并立即关闭。此外,我希望它能够处理没有扩展名的文件,并将它们作为与所需文件扩展名不匹配的文件传递出去。下面是执行此操作的代码片段,希望有人能向我解释我在这里做错了什么

File directory =  getFilesDir();
File[] listOfFiles = directory.listFiles();
String[] fileArray;
ListView fileList = (ListView) findViewById(R.id.fileList);
fileArray = new String[listOfFiles.length];
for (int i = 0; i < listOfFiles.length; ++i){
    String extension = MimeTypeMap.getFileExtensionFromUrl(listOfFiles[i].getAbsolutePath());
    if (extension.equals("txt")) {
        fileArray[i] = listOfFiles[i].getName();
    }

附言:我是Java和Android开发新手,所以请对我好一点。:)

编辑:

好的,我被要求发布我的错误,我真的不知道如何以一种好的方式发布它们,但这是我能做的最好的

03-29 15:39:32.437 7671-7671/?W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_0_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_0_apk.apk@上课。dex)因为非0退出状态 03-29 15:39:32.479 7671-7671/? W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_1_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_1_apk.apk@上课。dex)因为非0退出状态 03-29 15:39:32.522 7671-7671/? W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_2_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_2_apk.apk@上课。dex)因为非0退出状态 03-29 15:39:32.571 7671-7671/? W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_3_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_3_apk.apk@上课。dex)因为非0退出状态 03-29 15:39:32.618 7671-7671/? W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_4_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_4_apk.apk@上课。dex)因为非0退出状态 03-29 15:39:32.671 7671-7671/? W/art:Failed execv(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set=arm64--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--运行时arg-Xms64m--运行时arg-Xmx512m--指令集变量=通用--指令集功能=默认--索引文件=/data/app/com。为了隐私被移除。appname-1/split_lib_slice_5_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_5_apk.apk@上课。dex)因为非0退出状态 03-29 15:39:32.724 7671-7671/? W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_6_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_6_apk.apk@上课。dex)因为非0退出状态 03-29 15:39:32.766 7671-7671/? W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_7_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_7_apk.apk@上课。dex)因为非0退出状态 03-29 15:39:32.809 7671-7671/? W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_8_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_8_apk.apk@上课。dex)因为非0退出状态 03-29 15:39:32.884 7671-7671/? W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_9_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_9_apk.apk@上课。dex)因为非0退出状态 03-29 15:39:32.887 7671-7671/? W/System:ClassLoader引用了未知路径:/data/app/com。为了隐私被移除。appname-1/lib/arm64 03-29 15:39:32.894 7671-7671/? I/InstantRun:启动com的即时运行服务器。为了隐私被移除。appname 03-29 15:39:33.061 7671-7671/? I/HwCust:为类安卓找到构造函数。应用程序。hwcusthwmanagerimpl 03-29 15:39:33.276 7671-7671/? I/HwSecImmHelper:mSecurityInputMethodService为空 03-29 15:39:33.349 7671-7727/? I/OpenGLRenderer:初始化EGL,版本1.4 03-29 15:39:33.396 7671-7671/? W/art:Android 4.1之前,Android中的方法。支持v7。内部的小装置。ListViewCompat。lookForSelectablePosition(int,boolean)会错误地覆盖安卓中的包私有方法。小装置。列表视图 03-29 15:39:34.694 7671-7677/? I/art:忽略第二个调试器--接受并删除

以上是应用程序启动时的日志。这是在加载问题区域之前。下面是我按下按钮时触发问题区域运行的情况

03-29 15:42:50.550 10762-10762/?I/art:Late-enabling-Xcheck:jni 03-29 15:42:50.654 10762-10762/com。为了隐私被移除。appname W/art:Failed execv(/system/bin/dex2oat--runtime arg--classpath--runtime arg--debugable--instruction set=arm64--instruction set features=smp,a53--runtime-arg-Xrelocate--boot image=/system/framework/boot。art--运行时参数-Xms64m--运行时参数-Xmx512m--指令集变量=通用--指令集功能=默认--索引文件=/data/app/com。为了隐私被移除。appname-1/split_lib_dependencies_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_dependencies_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:50.808 10762-10762/com。为了隐私被移除。appname W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_0_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_0_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:50.85110762-10762/com。为了隐私被移除。appname W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_1_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_1_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:50.895 10762-10762/com。为了隐私被移除。appname W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_2_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_2_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:50.953 10762-10762/com。为了隐私被移除。appname W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_3_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_3_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:51.008 10762-10762/com。为了隐私被移除。appname W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_4_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_4_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:51.072 10762-10762/com。为了隐私被移除。appname W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_5_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为私隐而移除y、 appname-1@split_lib_slice_5_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:51.151 10762-10762/com。为了隐私被移除。appname W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_6_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_6_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:51.197 10762-10762/com。为了隐私被移除。appname W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_7_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_7_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:51.241 10762-10762/com。为了隐私被移除。appname W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_8_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_8_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:51.315 10762-10762/com。为了隐私被移除。appname W/art:执行失败(/system/bin/dex2oat--runtime arg-classpath--runtime arg--debugable--instruction set features=smp,a53--runtime arg-Xrelocate--boot image=/system/framework/boot.art--runtime arg-Xms64m--runtime arg-Xmx512m--instruction set variant=generic--instruction set features=default--dex file=/data/app/com.removedforpr常春藤。appname-1/split_lib_slice_9_apk。apk—oat文件=/data/dalvik cache/arm64/data@app@com。为了隐私被移除。appname-1@split_lib_slice_9_apk.apk@上课。dex)因为非0退出状态 03-29 15:42:51.318 10762-10762/com。为了隐私被移除。appname W/System:ClassLoader引用了未知路径:/data/app/com。为了隐私被移除。appname-1/lib/arm64 03-29 15:42:51.326 10762-10762/com。为了隐私被移除。appname I/InstantRun:正在启动com的即时运行服务器。为了隐私被移除。appname

最后一件事,当我按下按钮时,会显示另一个日志位,我认为这与上面的不同,但它很快就会消失,所以我无法复制它。希望有帮助


共 (3) 个答案

  1. # 1 楼答案

    使用ends with从整个字符串中获取扩展名

    File directory =  getFilesDir();
    File[] listOfFiles = directory.listFiles();
    String[] fileArray;
    
    fileArray = new String[listOfFiles.length];
     if (listOfFiles != null) {
    
            for (int i = 0; i < listOfFiles.length; ++i){
    
                 if (listOfFiles[i].getName().endsWith(".txt")){
                              //Do what ever u want
    
              }
            }   
        }       
    
  2. # 2 楼答案

    找到我的解决方案,MimeTypeMap不会给出正确的结果。因此,请使用您自己的方法从URL查找文件类型。检查我的解决方案

      public static String getFileType(String fileName) {
        String fileExt = "";
        try {
            if (fileName != null) {
                fileExt = fileName.substring(fileName.lastIndexOf("."));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileExt;
    }
    

    希望这会有所帮助。快乐编码

    更新

    看,我认为您的列表文件路径是字符串,即URI。所以你需要转换成URI。然后从该URI获取路径。执行以下操作

       String filePath=String.valueOf(filePath);
       Uri.parse(filePath).getPath();//Here you can get the path of the file which contains the extension of the file. 
    
  3. # 3 楼答案

    就个人而言,我更喜欢使用ApacheCommonsIO提供的FilenameUtils.getExtension(file_name)。您可以将此行添加到应用程序的build.gradle

    dependencies {
        compile 'org.apache.directory.studio:org.apache.commons.io:2.4'
    }
    

    然后在代码中使用它:

    for (int i = 0; i < listOfFiles.length; i++){
        String extension = FilenameUtils.getExtension(listOfFiles[i].getAbsolutePath());
        if (extension.equals("txt")) {
            fileArray[i] = listOfFiles[i].getName();
        }
    }