Rahul
Rahul

Reputation: 1667

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

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();

Upvotes: 5

Views: 13814

Answers (3)

Muhammad Aamir Ali
Muhammad Aamir Ali

Reputation: 21117

You can get the type of the file to recognize the whether a file type is photo or video.

        String ext = file.getName().substring(file.getName().indexOf(".") + 1);

            if (ext.equalsIgnoreCase("jpg")
                        || ext.equalsIgnoreCase("png")
                        || ext.equalsIgnoreCase("jpeg")) {
                  type = "photo";
            }

Upvotes: 0

g00dy
g00dy

Reputation: 6788

Take a look at this. By determining the MIME type of the file, you can assess what file you're dealing with. There's also this one, which can be used to determine the MIME type etc.

Upvotes: 0

Has AlTaiar
Has AlTaiar

Reputation: 4152

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);
}

Upvotes: 12

Related Questions