How to determine file types in Android

What is the best way to determine a file type in Android?

I want to check if the given file is an image, a music file, etc.


Asked by: Nicole462 | Posted: 25-01-2022






Answer 1

  1. Include some mime.types files in your assets folder (for example from /etc/mime.types).
  2. Include activation.jar (from JAF) in your build path.
  3. Use something like this:

    try {
        MimetypesFileTypeMap mftm = 
            new MimetypesFileTypeMap(getAssets().open("mime.types"));
        FileTypeMap.setDefaultFileTypeMap(mftm);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
  4. Then use FileDataSource.getContentType() to obtain file types.

Answered by: Adelaide463 | Posted: 26-02-2022



Answer 2

public static String getMimeType(String fileUrl) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
    final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    return (mimeType !=  null) ? mimeType : "unknown";
}

Answered by: Alberta968 | Posted: 26-02-2022



Answer 3

A common way is by their extension only, or you can parse the header, but it should be too much slower and more complex for your apps.

Answered by: Brad577 | Posted: 26-02-2022



Similar questions

android - How do I determine what is touched in 3D space from the screen?

How do I use gl.gluUnproject in my OpenGL ES 1.1 android app to determine what is selected when the user touches the screen? My understanding is that the touch event results in a line and I have to find the first "thing" it intersects with. Are there any tutorials on how to do this?


How do i determine the primary (android device) account data eg gmail?

I can get the device (owner's)phone via TelephonyManager tm = (TelephonyManager)ctx.getSystemService(ctx.TELEPHONY_SERVICE); phone = tm.getLine1Number(); But i am not sure how to determine the gmail or other account belonging to this user(device) How do you do that?


android - How can I determine if a geopoint is displayed in currently viewable area?

Say I have mapview control in my Android app. If I know a landmark exists at a certain latitude and longitude, how can I determine if that landmark is currently viewable on the user's screen? Is there a method for getting the coordinates for the top left and bottom right corners of the visible area?


Is there a way to determine android physical screen height in cm or inches?

I need to know exactly how big the screen is on the device in real units of length so I can calculate the acceleration due to gravity in pixels per millisecond. Is there a method somewhere in the Android API for this?


android - How to determine the View size right after Activity has been started

I want to do some data processing (in a thread) right after my main Activity starts. For that data processing to be started I also need to know a visible size of my main View (which is a LinearLayout currently, but whatever). I wonder how to do that. I.e. I wonder what is the entry point (callback?) I should use to be sure that my main View has been layed out, so i can use view.getWidth/getHeight? Is creati...


Android: How to determine character index of a touch event's position in TextView?

I have a TextView with an OnTouchListener. What I want is the character index the user is pointing to when I get the MotionEvent. Is there any way to get to the underlying font metrics of the TextView?


android - How do I determine what is touched in 3D space from the screen?

How do I use gl.gluUnproject in my OpenGL ES 1.1 android app to determine what is selected when the user touches the screen? My understanding is that the touch event results in a line and I have to find the first "thing" it intersects with. Are there any tutorials on how to do this?


Android: Determine when app is being finalized

I posted a question yesterday about determining when an app is being finalized vs destroyed for screen orientation change. Thanks to the answers I received I was able to resolve my problem with the screen orientation change. However, I am still running into a roadblock. This app I am working on logs into a website with an HttpClient. As long as the app remains in memory the HttpClient will retain the cookies from logging i...


How to determine the velocity of an Android device?

What is the easiest way to find out how fast the Android device is traveling? Also, is there a way to register an intent for speed? Example: intent if the device goes more than 20 miles an hour.


keyboard - How to determine the current IME in Android?

I have an application where I would like to warn the user if they are not using the default Android softkeyboard. (i.e. they are using Swype or some thing else). How can I check which input method they currently have selected?


android - Determine the contents of Linux Cache

On a Linux device (using Android [Eclair] with a 2.6.29 kernel), I seem to have some contents in my cache that never go away. I would like to be able to examine the contents of my cache to find the culprit. I've looked at mount for any tmpfs that may be causing it, but they are taking up very little space. What other types of things could be constantly using up cache? If there is no bui...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top