0

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;
}
Matthias Vanb
  • 923
  • 5
  • 14
  • 32

1 Answers1

1

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

musefan
  • 47,875
  • 21
  • 135
  • 185
  • This does it, indeed! Your solution sounds very logic.. do you think the solution with an array of strings would be faster too, or doesn't that matter? @musefan – Matthias Vanb Apr 26 '13 at 16:23
  • @MatthiasVanb: I am not sure if it would be faster or not, it probably wouldnt be much different either way. But it would mean you don't have to worry about the underscore. Do whichever you feel more comfortable doing, and which is easier to understand – musefan Apr 26 '13 at 16:24