Is it possible to get the resolution of a video in Android?

有些话、适合烂在心里 提交于 2019-12-02 20:44:33
Kurtis Nusbaum

You can use the MediaMetadataRetriever to retrieve resolution information about a video file. You'd use the extractMetadata() method and using the METADATA_KEY_VIDEO_HEIGHT and METADATA_KEY_VIDEO_WIDTH constants. So you'd do something like this:

MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(/* file descriptor or file path goes here */);
String height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);

Trick for API 10+:

MediaMetadataRetriever mRetriever = new MediaMetadataRetriever();
mRetriever.setDataSource("video_file_path");
Bitmap frame = mRetriever.getFrameAtTime();

int width = frame.getWidth();
int height = frame.getHeight();

We are simple taking a random frame from our video and detecting its width and height.

If you have Video Content URI then you can also use content provider query

Like

    Cursor mediaCursor = getContentResolver().query(
                        videoContentURI,
                        {MediaStore.Video.Media.RESOLUTION}, null, null, null);
        if (mediaCursor != null) {
                    if (mediaCursor.moveToFirst()) {
        //The resolution of the video file, formatted as "XxY"
        String resolution=mediaCursor.getString(mediaCursor
                                  .getColumnIndex(MediaStore.Video.Media.RESOLUTION));
    }
}

You can check following post to get ContentURI from video Physical Path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!