Question

I'm attempting to use onActivityResult in order to launch the Android Gallery in order to display either photos - or videos depending on the ImageButton selected.

The problem is when attempting to open a photo it's attempting to launch the video player and I'm unsure why.

P.S.

Videos can be launched an played successfully using the video ImageButton.

private static final int SELECT_PHOTO = 1;
private static final int SELECT_VIDEO = 1;


ImageButton pb = (ImageButton) findViewById(R.id.photos);
    pb.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, SELECT_PHOTO);
        }
    });

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

            Intent intentGallery = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intentGallery.setType("video/*");
            startActivityForResult(intentGallery, 1);

        }

    });
}

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

        }
    } else if (requestCode == SELECT_PHOTO) {

        Uri selectedImage = data.getData();
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(selectedImage, "image/*");
        startActivity(intent);
    }
Was it helpful?

Solution

private static final int SELECT_PHOTO = 1;
private static final int SELECT_VIDEO = 1;  

They need to be different, having a value "1" means they go into the same "if" onActivityResult().

Also, its playing only video as that is your first "if" condition inside onActivityResult(), which will always end up being true with:

startActivityForResult(intentGallery, 1);  
startActivityForResult(intent, SELECT_PHOTO);  

Pass both properly as you compare them. Try it:

private static final int SELECT_PHOTO = 1;
private static final int SELECT_VIDEO = 2;

startActivityForResult(intentGallery, SELECT_VIDEO);  
startActivityForResult(intent, SELECT_PHOTO);   

Should get some help from this

OTHER TIPS

SLECT_VIDEO and SELECT_PHOTO cannot be same....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top