Is there a built in way to get all of the media files (movies , music) on a computer in java?
            Asked
            
        
        
            Active
            
        
            Viewed 2,396 times
        
    2 Answers
6
            No, not build in. But, you could create a crawler that scans your entire hard drive for files with certain extensions ( or even better, MIME types ).
Check out this answer for an easy way to iterate through directories: https://stackoverflow.com/a/3154523/691606
See also Get the Mime Type from a File. Based on 1st 2 (of 3) techniques shown therein, is this:
import javax.activation.MimetypesFileTypeMap;
import java.io.File;
class MimeForMedia {
    public static void main(String[] args) {
        String[] types = {
            "png", "jpg", "gif", "tif", "bmp",  // image
            "snd", "wav", "mp3", "ogg", "au",   // sound
            "mov", "avi", "mpg", "flv"  // video
            };
        MimetypesFileTypeMap typeMap = new MimetypesFileTypeMap();
        for (String type : types) {
            String name = "a." + type;
            String bestType = typeMap.getContentType(name);
            if (bestType.equals("application/octet-stream")) {
                System.out.print( "Using URLConnection:  " );
                try {
                    bestType = (new File(name)).toURI().toURL()
                        .openConnection().getContentType();
                } catch(Exception e) {
                    System.out.println( e.getMessage() );
                }
            }
            System.out.println (type + "  " + bestType);
        }
    }
}
Here are the results on a Windows 7 PC.
Using URLConnection:  png  image/png
jpg  image/jpeg
gif  image/gif
tif  image/tiff
Using URLConnection:  bmp  content/unknown
Using URLConnection:  snd  audio/basic
wav  audio/x-wav
Using URLConnection:  mp3  content/unknown
Using URLConnection:  ogg  content/unknown
au  audio/basic
mov  video/quicktime
avi  video/x-msvideo
mpg  video/mpeg
Using URLConnection:  flv  content/unknown
Press any key to continue . . .
        Community
        
- 1
 - 1
 
        RadicalRaid
        
- 996
 - 9
 - 17
 
- 
                    *"( or even better, MIME types )."* Good idea, bu what J2SE class provides the MIME/content type? I could not see anything in `File` or `FileSystemView` that covered it. – Andrew Thompson May 04 '12 at 17:20
 - 
                    1You most likely need an external library. Check http://www.rgagnon.com/javadetails/java-0487.html for some examples. – RadicalRaid May 04 '12 at 17:22
 - 
                    1I added some code/ouptut that I found surprising in the following respects. 1) PNG was not in the map. 2) TIFF was. 3) BMP was not recognized in either the map or via `URLConnection`. 4) MP3 was not recognized by either. Huh. – Andrew Thompson May 04 '12 at 18:35
 - 
                    wow thanks a lot. this should be a lot better than the method i did by looking at extensions! – HAxxor May 04 '12 at 19:14
 - 
                    I would call this class "IndexForMedia". – djangofan Aug 19 '13 at 15:40
 
5
            
            
        Iterate on all the folders of your file system and look for files with the extensions you need (if you have time).
        talnicolas
        
- 13,885
 - 7
 - 36
 - 56