无法使用onActivityResult打开图像和视频 - 已编辑/已更新

时间:2014-04-18 13:05:32

标签: java android image video android-mediaplayer

我正在尝试完成一项非常简单的任务:

我需要能够点击图像按钮进行视频,从图库中选择并播放,然后单击图像按钮获取图像,选择图像并查看它 - 所有使用图库

如何实现这一目标?

我相信我需要修改我的onActivityResult,因为它目前没有按预期执行。

当前来源:

        ImageButton pb = (ImageButton) findViewById(R.id.photos);
        pb.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, SELECT_PHOTO);
            }
        });

        ImageButton vb = (ImageButton) findViewById(R.id.video);
        vb.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
    .show();
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("video/*");
                startActivityForResult(intent, SELECT_VIDEO);

            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedVideo = data.getData();
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(selectedVideo, "video/*, image/*");
                startActivity(Intent.createChooser(intent,
                        "Complete action using"));  



            }

        }

        ;


    }
}

编辑:

我已更新我的来源以反映最近的答案 - 但我仍然无法打开图片 - 我继续得到"无法播放视频"当他们想要观看图像时。

1 个答案:

答案 0 :(得分:0)

checkout this for video:

Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("video/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Video"), SELECT_VIDEO);

and this for images:

Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Images"), SELECT_PHOTO);


and in onActivityResult try this :

if (requestCode==SELECT_VIDEO) {

//take whatever video action

}else if(requestCode==SELECT_PHOTO){

//take whatever image action

}