In some cases, we are unable to read the metadata. To ensure that we get the width and height, it is best to create a Bitmap
using MediaMetadataRetriever
, then get the width and height from the created Bitmap
, as shown below:
public int getVideoWidthOrHeight(File file, String widthOrHeight) {
MediaMetadataRetriever retriever = null;
Bitmap bmp = null;
FileInputStream inputStream = null;
int mWidthHeight = 0;
try {
retriever = new MediaMetadataRetriever();
inputStream = new FileInputStream(file.getAbsolutePath());
retriever.setDataSource(inputStream.getFD());
bmp = retriever.getFrameAtTime();
if (widthOrHeight.equals("width")){
mWidthHeight = bmp.getWidth();
}else {
mWidthHeight = bmp.getHeight();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} finally{
if (retriever != null){
retriever.release()
}if (inputStream != null){
inputStream.close()
}
}
return mWidthHeight;
}
You can call the above method like this:
// Get the video width
int mVideoWidth = getVideoWidthOrHeight(someFile, "width");
// Get the video height
int mVideoHeight = getVideoWidthOrHeight(someFile, "height");