22

I'm trying to get the size of a remote video using this class and i'm getting IllegalArgumentException if the video is remote.

the video is an mp4 stored in one server...

the video plays correctly if i play it with mediaplayer, but it gives the error if i try to do this:

try {
    MediaMetadataRetriever retriever = new  MediaMetadataRetriever();
    Bitmap bmp = null;      
    retriever.setDataSource(context, uri);
    bmp = retriever.getFrameAtTime();           
    videoHeight = (int) (bmp.getHeight()*((float)getIntWidth()/bmp.getWidth()));
} catch (Exception e) {
    e.printStackTrace();
}   

the error is thrown in this line:

retriever.setDataSource(context, uri);

and uri contains Uri.parse("http://www.myweb.com/myvideo.mp4");

what is wrong in the code?

12-19 13:38:08.610: W/System.err(13333): java.lang.IllegalArgumentException
12-19 13:38:08.611: W/System.err(13333):    at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:175)
1
  • Did you resolve this issue?
    – ClassA
    Jan 28, 2018 at 5:21

6 Answers 6

26

Maybe you are running into this bug. If so try:

try {
    MediaMetadataRetriever retriever = new  MediaMetadataRetriever();
    Bitmap bmp = null;      
    retriever.setDataSource("http://www.myweb.com/myvideo.mp4", new HashMap<String, String>());
    bmp = retriever.getFrameAtTime();           
    videoHeight = (int) (bmp.getHeight()*((float)getIntWidth()/bmp.getWidth()));
} catch (Exception e) {
    e.printStackTrace();
}

If that doesn't work you can always try FFmpegMediaMetadataRetriever:

FFmpegMediaMetadataRetriever retriever = new  FFmpegMediaMetadataRetriever();

try {
    Bitmap bmp = null;      
    retriever.setDataSource("http://www.myweb.com/myvideo.mp4"));
    bmp = retriever.getFrameAtTime();           
    videoHeight = (int) (bmp.getHeight()*((float)getIntWidth()/bmp.getWidth()));
} catch (Exception e) {
    e.printStackTrace();
}

retriever.release();
6
  • The method setDataSource(String) in the type MediaMetadataRetriever is not applicable for the arguments (String, HashMap<String,String>) Dec 22, 2014 at 8:25
  • Method signature here: developer.android.com/reference/android/media/… Dec 22, 2014 at 16:23
  • FFmpegMediaMetadataRetriever only needs 5 MB of space. You should be making individual APK's for each architecture when using the NDK. Dec 22, 2014 at 16:23
  • I thought you can add the maven repo and use ffmpegmediametadataretriever, or did i missunderstood the concept? +1 For the possible answer May 3, 2015 at 21:09
  • That's is now the preferred method since Android Studio has replaced Eclipse. May 5, 2015 at 6:36
10

I was getting the same error, I am using android 10.

I solved just putting android:requestLegacyExternalStorage="true" in Manifest inside application.

See here

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:requestLegacyExternalStorage="true"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

1
  • I have already set the "android:requestLegacyExternalStorage="true" but still facing the same error Dec 15, 2020 at 7:36
3

In my case, I was creating a simple metadata extraction test app, so I copied a file to my phone using adb, like so:

adb push 350950598.mp4 /sdcard/Movies

but I forgot to add the read external storage directory permission in the app manifest.

Specifically:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.cool.package.name">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    ...
</manifest>

Adding those permissions fixed it for me, even for the simple file string call:

    MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
    mediaMetadataRetriever.setDataSource(movie.getPath());

And of course, if you're targeting API 23+ marshmallow then you'll have to dynamically ask for those permissions, as well.

0
3
try {
    MediaMetadataRetriever retriever = new  MediaMetadataRetriever();
    Bitmap bmp = null;      
    retriever.setDataSource(uri.toString(), new HashMap<String, String>());
    bmp = retriever.getFrameAtTime();           
    videoHeight = (int) (bmp.getHeight()*((float)getIntWidth()/bmp.getWidth()));
} catch (Exception e) {
    e.printStackTrace();
}   
2

if you are uses android 10 or above one then you need to mention requestLegacyExternalStorage to true.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:requestLegacyExternalStorage="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

Note : if by doing this still you're facing the same issue then you need to reinstall the app. :)

1

You need to give runtime permissions if you are using Android Marshmallow or later.

Android Manifest File:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.cool.package.name">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...

Then add code for runtime permissions in your activity. After that, run your application and it should work.

1
  • 2
    Why does it need WRITE_EXTERNAL_STORAGE ? Sep 12, 2020 at 17:51

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.