来自图库的视频选择:应仅限制最长30秒的视频

时间:2015-02-03 08:01:30

标签: android video duration onactivityresult

如何选择最长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();
            }
        }

1 个答案:

答案 0 :(得分:2)

试试希望它有所帮助

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