从图库中选择视频:最多只能选择 30 秒的视频

Video selection from gallery: Limit only videos up to 30 seconds should be selected

我怎样才能控制长达 30 秒的视频,否则应该选择显示 Toast/Popup。 我可以在 onActivityResult 中获取视频的路径并且可以 运行 视频但无法获取持续时间。任何建议,下面是我的代码:

case Utils.REQUEST_CODE_GALLERY_VIDEO:

        if (resultCode == Activity.RESULT_OK) {
            Uri selectedVieo = data.getData();
            String[] filePathColumn = { MediaStore.Video.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedVieo,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            try {
                if (filePath != null) {
                    runVideo(filePath);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

试试这个希望对你有帮助

case Utils.REQUEST_CODE_GALLERY_VIDEO:

        if (resultCode == Activity.RESULT_OK) {
            Uri selectedVieo = data.getData();
            String[] filePathColumn = { MediaStore.Video.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedVieo,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            try {
                if (filePath != null) {

                    MediaPlayer mp = MediaPlayer.create(this, Uri.parse(filePath));
                    int duration = mp.getDuration();
                    mp.release();

                    if((duration/1000) > 30){
                        // Show Your Messages                        
                    }else{
                        runVideo(filePath);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }