Skip to content Skip to sidebar Skip to footer

Android: Video Picker From Gallery

I want to Select video from gallery.I Have used intent to open the gallery but don't know what should be done onActivityResult for Videos. I am able to select Images just want to g

Solution 1:

Try to use the code below:

public class GalleryVideoPickerActivity extends Activity {
    private static final int SELECT_VIDEO = 1;

    private String selectedVideoPath;

    @ Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, SELECT_VIDEO); 
    }

    @ Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_VIDEO) {
                selectedVideoPath = getPath(data.getData());
                try {
                    if(selectedVideoPath == null) {
                        Log.e("selected video path = null!");
                        finish();
                    } else {
                            /**
                             * try to do something there
                             * selectedVideoPath is path to the selected video
                             */
                    }
                } catch (IOException e) {
                    //#debug
                    e.printStackTrace();
                }
            }
        }
        finish();
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor!=null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        else return null;
    }
}

Post a Comment for "Android: Video Picker From Gallery"