如何在从库中选择的活动中查看视频

时间:2014-11-20 02:29:39

标签: android video

我正在Android应用程序中实现视频上传功能。我可以从图库中选择一个视频但我无法在我的活动中查看它。我不知道如何将一个视频从图库放到一个活动的视频 我从库中选择视频的代码是:

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方法代码

@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();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

使用此代码

 @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();
            }
        }
    }

答案 1 :(得分:0)

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();