Question

I check my files in a folder on the sd card on certain extensions. Everything goes well, except for the .3gp files seem not to be recognized by this code although it is listed in the possible formats. Any idea what might be causing this?

public enum SupportedFileFormat {
    _3GP("3gp"), MP4("mp4"), M4A("m4a"), AAC("aac"), TS("ts"), FLAC("flac"), MP3(
            "mp3"), MID("mid"), XMF("xmf"), MXMF("mxmf"), RTTTL("rtttl"), RTX(
            "rtx"), OTA("ota"), IMY("imy"), OGG("ogg"), MKV("mkv"), WAV(
            "wav");

    private String filesuffix;

    SupportedFileFormat(String filesuffix) {
        this.filesuffix = filesuffix;
    }

    public String getFilesuffix() {
        return filesuffix;
    }
}

@SuppressLint("DefaultLocale")
private boolean checkExtension(String fileName) {
    String ext = getFileExtension(fileName);

    if (ext == null)
        return false;
    try {
        if (SupportedFileFormat.valueOf(ext.toUpperCase(Locale.US)) != null) {
                Log.i("extension", ext);
            return true;
        }
    } catch (IllegalArgumentException e) {
        return false;
    }
    return false;
}

public String getFileExtension(String fileName) {
    int i = fileName.lastIndexOf('.');
    if (i > 0) {
        return fileName.substring(i + 1);
    } else
        return null;
}
Was it helpful?

Solution

I would say it is because of the _ (underscore) in the enum name, it is unable to find a match. I am assuming this is because enum names cannot start with a number. Perhaps if you modify your code as follows it will work:

if(Character.isDigit(ext.charAt(0)))
{
    ext = "_" + ext;
}
if (SupportedFileFormat.valueOf(ext.toUpperCase(Locale.US)) != null) {
    Log.i("extension", ext);
    return true;
}

Basically, the code is looking to see if the extension starts with a number, if it does we add an underscore to the start to ensure it matches with our enum naming convention

Of course it would be a lot simpler if you just had an array of strings to hold your supported file formats, rather than an enum

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top