Renzo
Renzo

Reputation: 87

ImageView and VideoView on same activity

I'm trying to display a captured image on my MainActivity. The problem is it says that onActivityResult is already defined. My first onActivityResult is for the VideoView. Now I have to put a new onActivityResult for my image. How do I make this possible? Do I only have to have one onActivityResult? Do I have to put the second onActivityResult to the first onActivityResult?

Here's my code:

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

}

Upvotes: 1

Views: 1189

Answers (3)

Vinay
Vinay

Reputation: 767

create two view ImageView for Image and VideoView for Video and use this:

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

Upvotes: 3

barotia
barotia

Reputation: 428

You can not have two function with the same signature.

You have to have a single onActivityResult, and in the if statement, you have to handle the resoult.

Example

if(resultCode == Activity.RESULT_OK){    
      if (requestCode == CAMERA_REQUEST ) {
           ...    
      }   
      if (requestCode == REQUEST_VIDEO_CAPTURE ) {
           ...  
      } 
}

Upvotes: 0

Clement Osei Tano
Clement Osei Tano

Reputation: 832

You need only a single onActivityResult(int requestCode, int resultCode, Intent data) for your Activity class

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

Your onActivityResult should look like the above code. Every function can be declared with a the same types and order of parameters only once in a class.

Upvotes: 1

Related Questions