Question

I'm attempting to launch the gallery and select a video to play however nothing happens when I click a video within the gallery after it is launched - is there anything else I should add to this code to be able to do so?

Source:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
Was it helpful?

Solution

You need to Override onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SELECT_PHOTO) {
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedVideo = data.getData();
            // do something. play video using uri
        } 

    }
}

Edit:

Change

ImageButton vb = (ImageButton) findViewById(R.id.video);
    vb.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(UI.this, "Video Testimonial", Toast.LENGTH_LONG).show();
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("video/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);

        }
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == SELECT_PHOTO) {
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedVideoLocation = data.getData();

                    // do something
                }

            }
        }
    });

TO

 ImageButton vb = (ImageButton) findViewById(R.id.video);
    vb.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(UI.this, "Video Testimonial", Toast.LENGTH_LONG).show();
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("video/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);

        }
  });
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == SELECT_PHOTO) {
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedVideoLocation = data.getData();

                    // do something
                }

            }
        }

Edit 2:

@Override
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/*");
            startActivity(intent);

        } 

    }
}

Edit 3:

Full Code

public class MainActivity extends Activity {

        // Splash screen timer
        private static int SPLASH_TIME_OUT = 5000;
        private static final int SELECT_PHOTO = 100;
        private static final int SELECT_VIDEO = 100;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.fg);

                ImageButton ab = (ImageButton) findViewById(R.id.imageButton1);
                ab.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                Toast.makeText(MainActivity.this, "Audio Testimonial", Toast.LENGTH_LONG)
                                                .show();
                                Intent i = new Intent(
                                                Intent.ACTION_PICK,
                                                android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
                                startActivityForResult(i, 1);
                        }
                });

                ImageButton vb = (ImageButton) findViewById(R.id.imageButton2);
                vb.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                Toast.makeText(MainActivity.this, "Video Testimonial", Toast.LENGTH_LONG)
                                                .show();
                                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                                photoPickerIntent.setType("video/*");
                                startActivityForResult(photoPickerIntent, SELECT_VIDEO);

                        }
                });
        }

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

                        }

                }



//                ImageButton wb = (ImageButton) findViewById(R.id.imageButton3);
//                wb.setOnClickListener(new View.OnClickListener() {
//                        public void onClick(View v) {
//                                Toast.makeText(MainActivity.this, "Written Testimonial",
//                                                Toast.LENGTH_LONG).show();
//                                Intent intent = new Intent(MainActivity.this, Written.class);
//                                startActivity(intent);
//                        }
//                });

                ImageButton pb = (ImageButton) findViewById(R.id.imageButton4);
                pb.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                Toast.makeText(MainActivity.this, "Before and After Photos",
                                                Toast.LENGTH_LONG).show();
                                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                                photoPickerIntent.setType("image/*");
                                startActivityForResult(photoPickerIntent, SELECT_PHOTO);
                        }
                });
        }
}

fg.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton2"
        android:layout_alignParentTop="true"
        android:layout_marginTop="24dp"
        android:src="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="44dp"
        android:src="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton2"
        android:layout_below="@+id/imageButton2"
        android:layout_marginTop="52dp"
        android:src="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="80dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

Snaps

enter image description here

enter image description here

enter image description here

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