Java Android - 查找文件扩展名

Posted

技术标签:

【中文标题】Java Android - 查找文件扩展名【英文标题】:Java Android - finding file extension 【发布时间】:2017-08-22 23:41:37 【问题描述】:

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

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();
    

P.S:我是 Java 和 android 开发的新手,所以请善待我。 :)

编辑:

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

03-29 15:39:32.437 7671-7671/? W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm64 --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.removedforprivacy.appname-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_0_apk。 apk@classes.dex) 因为非 0 退出状态 03-29 15:39:32.479 7671-7671/? W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm64 --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.removedforprivacy.appname-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_1_apk。 apk@classes.dex) 因为非 0 退出状态 03-29 15:39:32.522 7671-7671/? W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm64 --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.removedforprivacy.appname-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_2_apk。 apk@classes.dex) 因为非 0 退出状态 03-29 15:39:32.571 7671-7671/? W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm64 --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.removedforprivacy.appname-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_3_apk。 apk@classes.dex) 因为非 0 退出状态 03-29 15:39:32.618 7671-7671/? W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm64 --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.removedforprivacy.appname-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_4_apk。 apk@classes.dex) 因为非 0 退出状态 03-29 15:39:32.671 7671-7671/? W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm64 --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.removedforprivacy.appname-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_5_apk。 apk@classes.dex) 因为非 0 退出状态 03-29 15:39:32.724 7671-7671/? W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm64 --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.removedforprivacy.appname-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_6_apk。 apk@classes.dex) 因为非 0 退出状态 03-29 15:39:32.766 7671-7671/? W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm64 --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.removedforprivacy.appname-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_7_apk。 apk@classes.dex) 因为非 0 退出状态 03-29 15:39:32.809 7671-7671/? W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm64 --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.removedforprivacy.appname-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_8_apk。 apk@classes.dex) 因为非 0 退出状态 03-29 15:39:32.884 7671-7671/? W/art: 失败的 execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm64 --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.removedforprivacy.appname-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_9_apk。 apk@classes.dex) 因为非 0 退出状态 03-29 15:39:32.887 7671-7671/? W/System:ClassLoader 引用了未知路径:/data/app/com.removedforprivacy.appname-1/lib/arm64 03-29 15:39:32.894 7671-7671/? I/InstantRun:为 com.removedforprivacy.appname 启动 Instant Run Server 03-29 15:39:33.061 7671-7671/? I/HwCust:为类 android.app.HwCustHwWallpaperManagerImpl 找到构造函数 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 之前,方法 int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) 会错误地覆盖 android.widget.ListView 中的包私有方法 03-29 15:39:34.694 7671-7677/? I/art:忽略第二个调试器——接受和丢弃

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

03-29 15:42:50.550 10762-10762/? I/art: 后期启用 -Xcheck:jni 03-29 15:42:50.654 10762-10762/com.removedforprivacy.appname W/art: execv 失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_dependencies_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:50.808 10762-10762/com.removedforprivacy.appname W/art:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_0_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:50.851 10762-10762/com.removedforprivacy.appname W/艺术:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_1_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:50.895 10762-10762/com.removedforprivacy.appname W/art:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_2_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:50.953 10762-10762/com.removedforprivacy.appname W/艺术:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_3_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:51.008 10762-10762/com.removedforprivacy.appname W/艺术:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_4_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:51.072 10762-10762/com.removedforprivacy.appname W/艺术:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_5_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:51.151 10762-10762/com.removedforprivacy.appname W/art:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_6_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:51.197 10762-10762/com.removedforprivacy.appname W/艺术:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_7_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:51.241 10762-10762/com.removedforprivacy.appname W/art:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_8_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:51.315 10762-10762/com.removedforprivacy.appname W/艺术:执行失败(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set =arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --指令集变体=通用 --instruction-set-features=default --dex-file=/data/app/com.removedforprivacy.appname-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache /arm64/data@app@com.removedforprivacy.appname-1@split_lib_slice_9_apk.apk@classes.dex) 因为非 0 退出状态 03-29 15:42:51.318 10762-10762/com.removedforprivacy.appname W/系统:类加载器引用未知路径:/data/app/com.removedforprivacy.appname-1/lib/arm64 03-29 15:42:51.326 10762-10762/com.removedforprivacy.appname I/InstantRun:为 com.removedforprivacy.appname 启动即时运行服务器

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

【问题讨论】:

你能发布你的崩溃吗? 对不起,我真的不明白你的意思。 Android Studio 中没有出现错误。当应用程序正在运行并且包含此代码的页面打开时,应用程序会立即崩溃。对不起,我真的不能再给你了。 点击android monitor底部栏android studio,然后从Android Monitor下拉菜单中选择error查看错误 很抱歉,它有点长,我不确定我应该放多少,而且它的格式也不是很好。 检查我的答案。如果有帮助,请接受并投票。 【参考方案1】:

找到我的解决方案,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. 

【讨论】:

感谢您的回答,但对我不起作用。您在上面的代码中看到我引用了 listOfFiles[i],这就是我传递给您的函数的内容。我已经尝试传递它的 getAbsolutePath() 版本和 getName() 版本,但恐怕它仍然会崩溃。 :(【参考方案2】:

就个人而言,我更喜欢使用 Apache Commons IO 提供的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();
    

【讨论】:

嗯,我希望我能弄清楚发生了什么。恐怕这也行不通。 :( @BoshJailey 发布您的错误信息,以便我们为您提供帮助。【参考方案3】:

使用结尾从整个字符串中获取扩展名

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

          
           
           

【讨论】:

再次感谢您的回答,但上述问题仍会导致崩溃。 :(

以上是关于Java Android - 查找文件扩展名的主要内容,如果未能解决你的问题,请参考以下文章

PECL轻松安装PHP扩展

旧光泽的 GKE 集群自动扩缩器配置文件

使用 Java 在文件夹中查找文件

同一段代码,扩展名为cpp就可以成功编译,扩展为c就不能编译,求解答

Android 存储访问框架 - 自定义文件类型的启动选择器

在 Unix 中查找所有具有特定扩展名的文件?