-3

How can I check if a video/image file is valid in Android. The image/video files are provided by querying the content provider.

I want to check if the image/video file can be decoded.

The picture with problems...

https://i.stack.imgur.com/2meiz.jpg

  • 1
    what u mean by video/image file is valid ? u can check if the selected file is image is not simple – ND1010_ Sep 07 '17 at 12:04
  • 1
    Valid... This means you can open it in the image gallery (the devices supports the image file). Same with video files. – Raul Tunduc Sep 07 '17 at 12:06
  • I think you are trying to validate the file formats like `.jpg`, `.mp4`, etc., right? – vss Sep 07 '17 at 12:09
  • Yes. This is what I want. – Raul Tunduc Sep 07 '17 at 12:10
  • @RaulTunduc you can check these for images :- https://stackoverflow.com/a/7388398/1518273 and videos :- https://stackoverflow.com/a/34440432/1518273 – Napolean Sep 07 '17 at 12:35
  • I want to check if the image/video file can be decoded – Raul Tunduc Sep 07 '17 at 12:45
  • @RaulTunduc can you link one or two corrupt image file(s) by [**editing your question**](https://stackoverflow.com/posts/46095821/edit)? _"cannot open file"_ is likely more fixable than _"file opens but with some missing/grey sections"_. – VC.One Sep 08 '17 at 00:53
  • @VC.One I've done it. Please take a look. Thank you! – Raul Tunduc Sep 08 '17 at 06:20
  • @RaulTunduc I need original file not a copy on _https://i.stack.Imgur.com_. Use a real file-sharing site like DropBox or even Google Drive (available with your Google acc). If still not sure, just email 1 or 2 images to me at : _valerio_charles@yahoo.com_. Your shared image shows a photo with grey areas (eg: **missing bytes**) but you say _"corrupted images in my phone which I cannot open using the phone's image gallery "_ which sounds like a **corrupt header** (where gallery **only shows error message**, not even any part of picture). – VC.One Sep 12 '17 at 11:10
  • On Android do you get same thing as [**the shared picture**](https://i.stack.imgur.com/2meiz.jpg)? **If YES**, the image is valid (can load) just that you don't like the grey colours? To a machine, that is the final picture... **If NO** then you must be getting an error message text like _"cannot display image"_? Try checking image bytes with a [**hex editor**](https://www.onlinehexeditor.com/) does it start `FF D8` and end with `FF D9` (scroll down to bottom)? If any of those is missing a viewer may not decode the jpeg. – VC.One Sep 12 '17 at 11:21

1 Answers1

1

For Image

public class IsImageFile implements FileFilter
{
        File file;
        private final String[] okFileExtensions =  new String[] {"jpg", "png", "gif","jpeg"};

        public ImageFileFilter(File newfile) 
        {
            this.file=newfile;
        }

        public boolean accept(File file) 
        {
             for (String extension : okFileExtensions)
                {
                  if (file.getName().toLowerCase().endsWith(extension))
                  {               
                    return true;
                  }
                }
            return false;         
        }

}

You can do like this for video also

Neha Sharma
  • 449
  • 5
  • 13