Monday, December 29, 2014

MediaMetadataRetriever: IllegalArgumentException thrown from setDataSource()

Here follow-up my old post "Get image frame in video using MediaMetadataRetriever", related of permission of "android.permission.READ_EXTERNAL_STORAGE" again.


It's a old exercise to retrieve image frame from mp4 video. At the time I wrote the post, it work as expected without need permission of "android.permission.READ_EXTERNAL_STORAGE". But now, you have to add it in AndroidManifest.xml. Otherwise, the code mediaMetadataRetriever.setDataSource(uri) will throw java.lang.IllegalArgumentException, caused by the path is invalid.


MainActivity.java

package com.example.androidmediametadataretriever;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

 String uri = "/storage/emulated/0/DCIM/LEDrose.mp4";
 ImageView capturedImageView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  capturedImageView = (ImageView) findViewById(R.id.capturedimage);

  MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();

  mediaMetadataRetriever.setDataSource(uri);
  Bitmap bmFrame = mediaMetadataRetriever.getFrameAtTime(5000000);
  capturedImageView.setImageBitmap(bmFrame);

 }

}

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidmediametadataretriever.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/capturedimage"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Permission of "android.permission.READ_EXTERNAL_STORAGE" is needed in AndroidManifest.xml. And minSdkVersion have to be 10 or higher to use MediaMetadataRetriever in your code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidmediametadataretriever"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

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

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

</manifest>

No comments: