1

I have not been able to find how to let the user choose a video from their videos, then playback the video in the app. How can I do this?

This is all the code I have for videos right now:

 Intent intent = new Intent( android.provider.MediaStore.ACTION_VIDEO_CAPTURE );
startActivityForResult(intent);

I don't know how to access the file the user chooses.

felix
  • 454
  • 9
  • 19

1 Answers1

1
  1. For choosing video :

    private static final int PICK_FROM_FILE = 1;
    
    btn_browse_vid.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            i.setType("video/*");
            startActivityForResult(i, PICK_FROM_FILE);
        }
    });
    
  2. xml for display video :

    <VideoView
      android:id="@+id/myVideo"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"/>
    
  3. onActivityResult :

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
    
      if (resultCode != RESULT_OK)
        return;
    
      Uri vidUri;
      if (requestCode == PICK_FROM_FILE) {
        vidUri = data.getData();
      } 
      //set the video path
      VideoView vidView = (VideoView)itemView.findViewById(R.id.myVideo);
      vidView.setVideoURI(vidUri);
    
      //media controller
      MediaController vidControl = new MediaController(YourActivity.this);
      vidControl.setAnchorView(vidView);
      vidView.setMediaController(vidControl);
    }
    
Faruk
  • 5,438
  • 3
  • 30
  • 46