title: Get the length of a video in Android
body: <p>I want to get the length of a saved video from my device. I have tried with <code>MediaMetadataRetriever</code> but my application crashes. I am testing it on Android 6.0 but it is not working at all. </p>
  
  <p>Below is my Activity.</p>
  
  <pre><code>    public class MainActivity extends AppCompatActivity {
  
      private static final int PICK_IMAGE_REQUEST = 1; // put it on the top.
      private Button btnOpenGallery;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          btnOpenGallery = (Button)findViewById(R.id.btnOpenGallery);
          btnOpenGallery.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  Intent intent = new Intent();
                  intent.setType("video/*");
                  intent.setAction(Intent.ACTION_GET_CONTENT);
                  startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
  
              }
          });
  
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if (requestCode == PICK_IMAGE_REQUEST &amp;&amp; resultCode == RESULT_OK &amp;&amp; data != null &amp;&amp; data.getData() != null) {
  
              Uri selectedImageUri = data.getData();
              String strPath = selectedImageUri.getPath();
              Log.e("Path is ","===&gt;"+strPath);
  
  
              MediaMetadataRetriever retriever = new MediaMetadataRetriever();
              retriever.setDataSource(strPath); // Enter Full File Path Here
              String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
              long timeInmillisec = Long.parseLong(time);
              Log.e("Media duration in ","==&gt;"+timeInmillisec);
  
          }
      }
  }
  </code></pre>