user1041858
user1041858

Reputation: 957

Android check for valid video file

How to check a video file is valid or not without checking its extension(.mp3 or .3gp etc). Means how to check the video file on SD card is supported by device or not?

Is there any api to validate video file in android 4.0 and above?

My Scenario: I am playing video on VideoView after downloading it and play it from local SD card after download success. Next time when a request for same video, then checks in SD card, if found then start playing it(No download in this case). But sometimes network error or app kill interrupt the downloading(in this case the video file is not completely downloaded) so the downloaded file is corrupted and VideoView is unable to play this file. So how to detect this corrupted file.

Upvotes: 4

Views: 7419

Answers (3)

Manthan Patel
Manthan Patel

Reputation: 1852

@Alex had given right answer but still some problems are there as like @Kirill mention in comment that setDataSource often throws java.lang.RuntimeException: setDataSource failed exception. So Here is the function check for valid video file

private boolean videoFileIsCorrupted(String path){

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();

    try {
        retriever.setDataSource(myContext, Uri.parse(path));
    }catch (Exception e){
        e.printStackTrace();
        return false;
     }

String hasVideo = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
return "yes".equals(hasVideo);
}

Upvotes: 2

Alex
Alex

Reputation: 1349

Here is the code that worked for me:

 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
 retriever.setDataSource(context, Uri.fromFile(fileToTest));

 String hasVideo = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
 boolean isVideo = "yes".equals(hasVideo);

Upvotes: 15

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

I think this will be useful, The method is an older one that i used for other purpose just modified a bit for your use, Try it ,May work

private void checkAndLoadFile(
        File currentFile) {
            String path = currentFile.getAbsolutePath();
    String extension = Utility.getExtension(path);
    MimeTypeMap mimeMap = MimeTypeMap.getSingleton();

    if(mimeMap.hasExtension(extension))
    {
        String mimeType = mimeMap.getMimeTypeFromExtension(extension);

        Intent viewFile = new Intent(Intent.ACTION_VIEW);
        viewFile.setDataAndType(Uri.fromFile(currentFile), mimeType);

        PackageManager pm = getPackageManager();
        List<ResolveInfo> apps = 
                pm.queryIntentActivities(viewFile, PackageManager.MATCH_DEFAULT_ONLY);

        if (apps.size() > 0)
            startActivity(viewFile); //This video is supported and there are apps installed in this device to open the video
        else
            showAsNotSupported();
    } else
        showAsNotSupported();
}

Upvotes: 0

Related Questions