ImageView 和 VideoView 在同一个 activity
ImageView and VideoView on same activity
我正在尝试在我的 MainActivity 上显示捕获的图像。问题是它说 onActivityResult 已经定义了。我的第一个 onActivityResult 是针对 VideoView。现在我必须为我的图像添加一个新的 onActivityResult。我如何使这成为可能?我只需要一个 onActivityResult 吗?我是否必须将第二个 onActivityResult 放在第一个 onActivityResult 上?
这是我的代码:
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
Button buttonPlay;
Button buttonFullScreen;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
resultvideo = (VideoView)findViewById(R.id.videoView);
mediacontroller = new MediaController(MainActivity.this);
mediacontroller.setAnchorView(resultvideo);
resultvideo.setMediaController(mediacontroller);
Button click = (Button)findViewById(R.id.buttonRecord);
resultvideo = (VideoView)findViewById(R.id.videoView);
this.imageView = (ImageView)this.findViewById(R.id.imageView);
Button photoButton = (Button) this.findViewById(R.id.buttonCapture);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
public void dispatchTakeVideoIntent(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final Uri videoUri = data.getData();
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonPlay = (Button) findViewById(R.id.buttonPlay);
{
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
}
});
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("VIDEO_URI", videoUri.toString());
startActivity(intent);
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
您的 Activity class
只需要一个 onActivityResult(int requestCode, int resultCode, Intent data)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
break;
case REQUEST_VIDEO_CAPTURE:
//bring your video stuff here
break;
}
}
您的Activity结果应该与上面的代码类似。每个函数只能在 class.
中使用相同类型和顺序的参数声明一次
不能有两个具有相同签名的函数。
您必须有一个 onActivityResult,并且在 if 语句中,您必须处理结果。
示例
if(resultCode == Activity.RESULT_OK){
if (requestCode == CAMERA_REQUEST ) {
...
}
if (requestCode == REQUEST_VIDEO_CAPTURE ) {
...
}
}
为图像创建两个视图 ImageView 并为视频创建 VideoView 并使用它:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Hide video view
videoview.setVisibility(View.GONE);
}
break;
case REQUEST_VIDEO_CAPTURE:
//bring your video stuff here
videoview.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
break;
}
}
我正在尝试在我的 MainActivity 上显示捕获的图像。问题是它说 onActivityResult 已经定义了。我的第一个 onActivityResult 是针对 VideoView。现在我必须为我的图像添加一个新的 onActivityResult。我如何使这成为可能?我只需要一个 onActivityResult 吗?我是否必须将第二个 onActivityResult 放在第一个 onActivityResult 上?
这是我的代码:
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
Button buttonPlay;
Button buttonFullScreen;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
resultvideo = (VideoView)findViewById(R.id.videoView);
mediacontroller = new MediaController(MainActivity.this);
mediacontroller.setAnchorView(resultvideo);
resultvideo.setMediaController(mediacontroller);
Button click = (Button)findViewById(R.id.buttonRecord);
resultvideo = (VideoView)findViewById(R.id.videoView);
this.imageView = (ImageView)this.findViewById(R.id.imageView);
Button photoButton = (Button) this.findViewById(R.id.buttonCapture);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
public void dispatchTakeVideoIntent(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final Uri videoUri = data.getData();
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonPlay = (Button) findViewById(R.id.buttonPlay);
{
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
}
});
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("VIDEO_URI", videoUri.toString());
startActivity(intent);
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
您的 Activity class
只需要一个onActivityResult(int requestCode, int resultCode, Intent data)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
break;
case REQUEST_VIDEO_CAPTURE:
//bring your video stuff here
break;
}
}
您的Activity结果应该与上面的代码类似。每个函数只能在 class.
中使用相同类型和顺序的参数声明一次不能有两个具有相同签名的函数。
您必须有一个 onActivityResult,并且在 if 语句中,您必须处理结果。
示例
if(resultCode == Activity.RESULT_OK){
if (requestCode == CAMERA_REQUEST ) {
...
}
if (requestCode == REQUEST_VIDEO_CAPTURE ) {
...
}
}
为图像创建两个视图 ImageView 并为视频创建 VideoView 并使用它:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Hide video view
videoview.setVisibility(View.GONE);
}
break;
case REQUEST_VIDEO_CAPTURE:
//bring your video stuff here
videoview.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
break;
}
}