5

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();
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
Rahul
  • 1,667
  • 6
  • 21
  • 38

3 Answers3

12

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);
}
Community
  • 1
  • 1
Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37
  • Ok by getting the mine type. problem is solve, But there are some issue. Like suppose in my device i have .mp4 file and .flv video file. Now for mp4 file the mine type return is video..but for flv file mine type always return null. – Rahul Jul 11 '13 at 08:04
  • This is also works if tried with local file paths such as `"/sdcard/path/to/video.extension"`. The problem is if the local file contains space in its path, it always returns null – nmxprime Dec 31 '15 at 05:10
0

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.

g00dy
  • 6,752
  • 2
  • 30
  • 43
0

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";
            }
John61590
  • 1,106
  • 1
  • 13
  • 29
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • 2
    This method is inefficient because there are too many file extensions; it is almost impossible to cater for them all. – Taslim Oseni Jan 14 '18 at 01:42