从代码创建VideoView

时间:2013-05-16 05:53:08

标签: android android-layout android-video-player

我尝试从我的代码创建ViedoView但是当我运行它时,我没有看到任何视图创建。 我在我的数据库中保存视频的路径,然后尝试显示它。 我尝试使用ImageView和它的工作相同,所以我想我忘了VideoView中的东西。

我的代码:

    private void getUserVideo() {

        Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
        intentToPlayVideo.setType("video/*");
        intentToPlayVideo.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intentToPlayVideo, SELECT_VIDEO);
    }


    public void onActivityResult(int requestCode, int resultCode, 
           Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        switch(requestCode) { 
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(
                                   selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

                extras = getIntent().getExtras();
                String s = extras.getString("date");

               DataBaseMain data = new DataBaseMain(this);
               data.open();
               data.putWorkOutPic(filePath, s);
               data.close();   
               recreate();
            }   

        case SELECT_VIDEO:
        {
            if(resultCode == RESULT_OK){ 
            Uri vid = imageReturnedIntent.getData();
            String videoPath = getRealPathFromURI(vid);

            extras = getIntent().getExtras();
            String s = extras.getString("date");

           DataBaseMain data = new DataBaseMain(this);
           data.open();
           data.putWorkOutVideo(videoPath, s);
           data.close();   
           recreate();
            }
        } 
     }
}


public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }


    private void addVideos() {

        extras = getIntent().getExtras();
        String s = extras.getString("date");

        DataBaseMain data = new DataBaseMain(this);
        data.open();
        videos = data.getWorkOutVideo(s);
        data.close();

        if(videos == null || videos.length < 1)
        return;

        LinearLayout linear = (LinearLayout) findViewById(R.id.dateMain);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);

        for(int i = 0; i < videos.length; i++){

            VideoView video = new VideoView(this);
            video.setVideoPath(videos[i]);
            video.setLayoutParams(lp);
            video.setId(i+50);
            video.setOnLongClickListener(new OnLongClickListener() {
                public boolean onLongClick(View arg0) {

                    selectedVideo = arg0.getId();
                    onButtonClickEvent(arg0);

                    return true;   
                }
            });
            linear.addView(video);
        }           
    }

1 个答案:

答案 0 :(得分:0)

看看Youtube Player这基本上是一个youtube播放器,但在此文件中看到setupView方法,它以编程方式创建代码,您可以知道哪里出错了