MediaMetadataRetriever setDataSource throws IllegalArgumentException

后端 未结 5 1691
一个人的身影
一个人的身影 2020-12-01 14:18

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 se

相关标签:
5条回答
  • 2020-12-01 15:05

    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>
    

    0 讨论(0)
  • 2020-12-01 15:06

    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();
    
    0 讨论(0)
  • 2020-12-01 15:08
    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();
    }   
    
    0 讨论(0)
  • 2020-12-01 15:14

    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.

    0 讨论(0)
  • 2020-12-01 15:17

    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 讨论(0)
提交回复
热议问题