Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether a file is an image, video or audio file

Tags:

java

file

android

I am using the following code to get files from gallery. The file that i retrieve is either image file, video file or an audio file based on user selection.

Now i am displaying the file retrieve from the gallery inside a list view. But i am not able to distinguish that the file selected by the user is image file(i.e .jpg / .png) or its an video file or an audio file.

By getting the extension and checking it in the if else condition its possible i know. But i want to know its there any possible way of doing this

Code used to get image from Gallery is

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(intent, GET_VIDEO_AUDIO);    
                            fileTransferDialog.dismiss();
like image 931
Rahul Avatar asked Jun 20 '13 11:06

Rahul


1 Answers

Just check the MIME type of the file. Have a look at this

https://stackoverflow.com/a/13889946/1570662

This might help

private static String getMimeType(String fileUrl) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
like image 88
Has AlTaiar Avatar answered Sep 26 '22 04:09

Has AlTaiar