Bhargav Thanki
Bhargav Thanki

Reputation: 4954

how to view video in activity that is chosen from the gallery

I am implementing video uploading functionality in an android application.I can choose a video from gallery but I can not view it in my activity.I dont know how to put a video from gallery to videoview of an activity My code to select video from gallery is :

mChoose.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("video/*");
            startActivityForResult(intent, REQUEST_ID);
        }
    });

onActivityResult Method code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    InputStream stream = null;
    if(requestCode == REQUEST_ID && resultCode == Activity.RESULT_OK)
    {
        try
        {
            stream = getContentResolver().openInputStream(data.getData());
            //System.out.println(data.getData());
            mVideo.setVideoPath(path);
            path = getRealPathFromURI(getApplicationContext(), data.getData());
            //getRealPathFromURI is method in class to obtain path from uri
            System.out.println(path);

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        if(stream != null)
        {
            try
            {
                stream.close();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

Upvotes: 1

Views: 2605

Answers (2)

Tech_Intelliswift
Tech_Intelliswift

Reputation: 516

Use this Code

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mVideo = (VideoView) findViewById(R.id.videoView);
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("video/*");
        startActivityForResult(intent, 1);
    }
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == 1 && resultCode == Activity.RESULT_OK)
        {
            try
            {
                String path = data.getData().toString();
                mVideo.setVideoPath(path);
                mVideo.requestFocus();
                mVideo.start();

            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }

Upvotes: 1

Vibhor Chopra
Vibhor Chopra

Reputation: 657

VideoView videoView = (VideoView) findViewById(R.id.videoview);
Uri vidFile = Uri.parse(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "path of the video");

    videoView.setVideoURI(vidFile);
    videoView
            .setMediaController(new MediaController(PlayVideoActivity.this));
    videoView.setVisibility(1);
    videoView.bringToFront();
    videoView.requestFocus();

Upvotes: 0

Related Questions