在VideoView中的图库中显示视频

时间:2012-06-12 02:51:38

标签: android

我的用户界面有一个按钮和一个视频。当我们点击按钮时,转到图库视频,当我们在视频库中选择视频时,它会返回我的用户界面,视频视图将显示已选择的视频。我使用的代码如下但它不显示视频:(

public class VideoGalleryActivity extends Activity {
/** Called when the activity is first created. */
Button button;
VideoView videoView;
private static final int PICK_FROM_GALLERY=1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button=(Button)findViewById(R.id.button);
    videoView=(VideoView)findViewById(R.id.videoview);

    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();

            intent.setType("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
        }
    })  ;      
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode != RESULT_OK) return;

    if (requestCode == PICK_FROM_GALLERY) {
        Uri mVideoURI = data.getData();  
        videoView.setVideoURI(mVideoURI);
    }

}

1 个答案:

答案 0 :(得分:1)

您必须通过添加videoview.start();来开始视频观看。代码:

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode != RESULT_OK) return;

    if (requestCode == PICK_FROM_GALLERY) {
        Uri mVideoURI = data.getData();  
        videoView.setVideoURI(mVideoURI);
        videoview.start();   //edited
    }

}