Reputation: 113
So I'm trying to get the extension from a file so that I can compare it to a string to see if the extension matches the extension I want to find. However, at the moment I think I am able to get the extension but then comparing it with the one I want to find is causing my app to crash and close immediately. Also, I want it to be able to cope with files that don't have extensions and just pass them off as files that don't match the wanted file extension. Below is the code snippet that performs this, hopefully someone can explain to me what I'm doing wrong here.
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: I'm new to Java and Android Development so please be nice to me. :)
EDIT:
Okay, I'm being asked to post my errors, I don't really know how to post them in a nice way but this is the best I can do.
03-29 15:39:32.437 7671-7671/? W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:39:32.479 7671-7671/? W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:39:32.522 7671-7671/? W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:39:32.571 7671-7671/? W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:39:32.618 7671-7671/? W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:39:32.671 7671-7671/? W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:39:32.724 7671-7671/? W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:39:32.766 7671-7671/? W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:39:32.809 7671-7671/? W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:39:32.884 7671-7671/? W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:39:32.887 7671-7671/? W/System: ClassLoader referenced unknown path: /data/app/com.removedforprivacy.appname-1/lib/arm64 03-29 15:39:32.894 7671-7671/? I/InstantRun: Starting Instant Run Server for com.removedforprivacy.appname 03-29 15:39:33.061 7671-7671/? I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl 03-29 15:39:33.276 7671-7671/? I/HwSecImmHelper: mSecurityInputMethodService is null 03-29 15:39:33.349 7671-7727/? I/OpenGLRenderer: Initialized EGL, version 1.4 03-29 15:39:33.396 7671-7671/? W/art: Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 03-29 15:39:34.694 7671-7677/? I/art: Ignoring second debugger -- accepting and dropping
The above is the log from when the app is started. This is before the problem area is loaded. The below is when I press the button that triggers the running of the problem area.
03-29 15:42:50.550 10762-10762/? I/art: Late-enabling -Xcheck:jni 03-29 15:42:50.654 10762-10762/com.removedforprivacy.appname W/art: Failed 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_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:50.808 10762-10762/com.removedforprivacy.appname W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:50.851 10762-10762/com.removedforprivacy.appname W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:50.895 10762-10762/com.removedforprivacy.appname W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:50.953 10762-10762/com.removedforprivacy.appname W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:51.008 10762-10762/com.removedforprivacy.appname W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:51.072 10762-10762/com.removedforprivacy.appname W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:51.151 10762-10762/com.removedforprivacy.appname W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:51.197 10762-10762/com.removedforprivacy.appname W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:51.241 10762-10762/com.removedforprivacy.appname W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:51.315 10762-10762/com.removedforprivacy.appname W/art: Failed 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@[email protected]@[email protected]) because non-0 exit status 03-29 15:42:51.318 10762-10762/com.removedforprivacy.appname W/System: ClassLoader referenced unknown path: /data/app/com.removedforprivacy.appname-1/lib/arm64 03-29 15:42:51.326 10762-10762/com.removedforprivacy.appname I/InstantRun: Starting Instant Run Server for com.removedforprivacy.appname
One last thing, when I press the button, there is another bit of logs shown which I think is different to the above but it very quickly disappears so I can't copy it in. Hope that helps.
Upvotes: 0
Views: 362
Reputation: 3444
Find my solution, MimeTypeMap will not give some proper result. So, do your own method to find the file type from the URL. Check my solution.
public static String getFileType(String fileName) {
String fileExt = "";
try {
if (fileName != null) {
fileExt = fileName.substring(fileName.lastIndexOf("."));
}
} catch (Exception e) {
e.printStackTrace();
}
return fileExt;
}
Hopes it will help. Happy Coding
UPDATE
See, I think your list file path is string which is URI. so you need to convert into URI . then get the path from that URI. Do the following.
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.
Upvotes: 0
Reputation: 15165
Personally, I prefer to use FilenameUtils.getExtension(file_name)
provided by Apache Commons IO. You can add this line to your app's build.gradle
:
dependencies {
compile 'org.apache.directory.studio:org.apache.commons.io:2.4'
}
And then use it in your code:
for (int i = 0; i < listOfFiles.length; i++){
String extension = FilenameUtils.getExtension(listOfFiles[i].getAbsolutePath());
if (extension.equals("txt")) {
fileArray[i] = listOfFiles[i].getName();
}
}
Upvotes: 0
Reputation: 912
Use ends with to get extension from whole string
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
}
}
}
Upvotes: 0